diff --git a/h2o/tags.php b/h2o/tags.php index ea2fdbf..f132c7e 100644 --- a/h2o/tags.php +++ b/h2o/tags.php @@ -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)) { @@ -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) { @@ -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); + } } } diff --git a/spec/loader_spec.php b/spec/loader_spec.php index 3b50bc3..07a7703 100644 --- a/spec/loader_spec.php +++ b/spec/loader_spec.php @@ -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 { @@ -102,4 +110,4 @@ function should_read_sub_template_in_include_tag() { expects($this->h2o->render())->should_match('/page menu/'); } } -?> \ No newline at end of file +?> diff --git a/spec/templates/dynamic-1.tpl b/spec/templates/dynamic-1.tpl new file mode 100644 index 0000000..5bb8c31 --- /dev/null +++ b/spec/templates/dynamic-1.tpl @@ -0,0 +1 @@ +DYNAMIC TEMPLATE #1 diff --git a/spec/templates/dynamic-2.tpl b/spec/templates/dynamic-2.tpl new file mode 100644 index 0000000..a074286 --- /dev/null +++ b/spec/templates/dynamic-2.tpl @@ -0,0 +1 @@ +DYNAMIC TEMPLATE #2 diff --git a/spec/templates/dynamic_include.tpl b/spec/templates/dynamic_include.tpl new file mode 100644 index 0000000..c34e477 --- /dev/null +++ b/spec/templates/dynamic_include.tpl @@ -0,0 +1,4 @@ +{% for i in 1,2 %} + {% include 'dynamic-{{ i }}.tpl' %} +{% endfor %} +