Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions h2o/tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ class Include_Tag extends H2o_Node {
private $nodelist;
private $syntax = '/^["\'](.*?)["\'](\s+with\s+(.+))?$/';
private $_additional_context = array();
private $dynamic = False;
private $fname_tpl;
private $tpl;
private $loader;

function __construct($argstring, $parser, $position = 0) {
if (!preg_match($this->syntax, $argstring, $matches)) {
Expand All @@ -294,11 +298,18 @@ function __construct($argstring, $parser, $position = 0) {
}

$this->filename = stripcslashes($matches[1]);
$this->nodelist = $parser->runtime->loadSubTemplate($this->filename, $parser->options);
$parser->storage['templates'] = array_merge(
$this->nodelist->parser->storage['templates'], $parser->storage['templates']
);
$parser->storage['templates'][] = $this->filename;
$this->fname_tpl = h2o::parseString($this->filename,$parser->options);
if ( $this->fname_tpl->render() != $this->filename ) {
$this->dynamic = True;
$this->loader = $parser->runtime;
} else {
$this->dynamic = False;
$this->nodelist = $parser->runtime->loadSubTemplate($this->filename, $parser->options);
$parser->storage['templates'] = array_merge(
$this->nodelist->parser->storage['templates'], $parser->storage['templates']
);
$parser->storage['templates'][] = $this->filename;
}
}

function render($context, $stream) {
Expand All @@ -312,7 +323,13 @@ function render($context, $stream) {
$context[$key] = $value;
}

$this->nodelist->render($context, $stream);
if ($this->dynamic) {
$fname = $this->fname_tpl->render($context);
$this->tpl = $this->loader->loadTemplate($fname);
$this->tpl->render($context,$stream);
} else {
$this->nodelist->render($context, $stream);
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion spec/loader_spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ function should_invalidate_cache_if_any_subtemplates_has_updated() {
expects($h2o->loader->cached)->should_be(false);
$h2o->loader->flush_cache();
}

function should_include_dynamic_filenames_in_include_tag() {
$opt = array('searchpath' => dirname(__FILE__).DS.'templates');

$h2o = h2o('dynamic_include.tpl',$opt);
expects($h2o->render())->should_match('/DYNAMIC TEMPLATE #1/');
expects($h2o->render())->should_match('/DYNAMIC TEMPLATE #2/');
}
}

class Describe_hash_loader extends SimpleSpec {
Expand Down Expand Up @@ -102,4 +110,4 @@ function should_read_sub_template_in_include_tag() {
expects($this->h2o->render())->should_match('/page menu/');
}
}
?>
?>
1 change: 1 addition & 0 deletions spec/templates/dynamic-1.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DYNAMIC TEMPLATE #1
1 change: 1 addition & 0 deletions spec/templates/dynamic-2.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DYNAMIC TEMPLATE #2
4 changes: 4 additions & 0 deletions spec/templates/dynamic_include.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% for i in 1,2 %}
{% include 'dynamic-{{ i }}.tpl' %}
{% endfor %}