From 218efdffe6afeef7167b62581b47b77db8660937 Mon Sep 17 00:00:00 2001 From: Ivan Shalganov Date: Thu, 10 Apr 2014 01:49:15 +0400 Subject: [PATCH] Fix #75 for new inheritance system --- sandbox/fenom.php | 2 +- sandbox/templates/extends/75-child.tpl | 13 +++++++++++++ sandbox/templates/extends/75-parent.tpl | 12 ++++++++++++ src/Fenom.php | 2 ++ src/Fenom/Render.php | 2 +- src/Fenom/Template.php | 4 +++- 6 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 sandbox/templates/extends/75-child.tpl create mode 100644 sandbox/templates/extends/75-parent.tpl diff --git a/sandbox/fenom.php b/sandbox/fenom.php index 39ff1f3..5f5bc5c 100644 --- a/sandbox/fenom.php +++ b/sandbox/fenom.php @@ -13,7 +13,7 @@ namespace { $fenom = Fenom::factory(__DIR__.'/templates', __DIR__.'/compiled', Fenom::FORCE_COMPILE); - $fenom->display("greeting.tpl", array( + $fenom->display("extends/75-child.tpl", array( "user" => array( "name" => "Ivka", 'type' => 'new' diff --git a/sandbox/templates/extends/75-child.tpl b/sandbox/templates/extends/75-child.tpl new file mode 100644 index 0000000..e99ff4b --- /dev/null +++ b/sandbox/templates/extends/75-child.tpl @@ -0,0 +1,13 @@ +{extends 'extends/75-parent.tpl'} +{block 'child'} + {macro child_test(v, i)} + child test - {$v}, i = {$i};
+ {var $i = $i -1} + {if $i > 0} + {macro.child_test v=$v i=$i} + {/if} + {/macro} + + child call:
+ {macro.child_test v = 'ok' i = 5} +{/block} \ No newline at end of file diff --git a/sandbox/templates/extends/75-parent.tpl b/sandbox/templates/extends/75-parent.tpl new file mode 100644 index 0000000..3f360b0 --- /dev/null +++ b/sandbox/templates/extends/75-parent.tpl @@ -0,0 +1,12 @@ +{macro parent_test(v, i)} + parent test - {$v}, i = {$i};
+{var $i = $i -1} +{if $i > 0} + {macro.parent_test v=$v i=$i} +{/if} +{/macro} + +{block 'child'}{/block} + +parent call:
+{macro.parent_test v = 'ok' i = 5}
\ No newline at end of file diff --git a/src/Fenom.php b/src/Fenom.php index cb6bd13..0edfbbd 100644 --- a/src/Fenom.php +++ b/src/Fenom.php @@ -741,6 +741,7 @@ class Fenom public function getTemplate($template, $options = 0) { $options |= $this->_options; +// var_dump($this->_options & self::FORCE_COMPILE); if (is_array($template)) { $key = dechex($options) . "@" . implode(",", $template); } else { @@ -755,6 +756,7 @@ class Fenom return $tpl; } } elseif ($this->_options & self::FORCE_COMPILE) { + return $this->compile($template, $this->_options & self::DISABLE_CACHE & ~self::FORCE_COMPILE, $options); } else { return $this->_storage[$key] = $this->_load($template, $options); diff --git a/src/Fenom/Render.php b/src/Fenom/Render.php index 536a973..6f9afa3 100644 --- a/src/Fenom/Render.php +++ b/src/Fenom/Render.php @@ -202,7 +202,7 @@ class Render extends \ArrayObject public function getMacro($name) { if (empty($this->_macros[$name])) { - throw new \RuntimeException('macro not found'); + throw new \RuntimeException('macro '.$name.' not found'); } return $this->_macros[$name]; } diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index 82c35f5..b5ebda1 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -471,6 +471,7 @@ class Template extends Render { if (!$this->_code) { // evaluate template's code + var_dump($this->_getClosureSource(), $this->_getMacrosArray()); eval("\$this->_code = " . $this->_getClosureSource() . ";\n\$this->_macros = " . $this->_getMacrosArray() . ';'); if (!$this->_code) { throw new CompileException("Fatal error while creating the template"); @@ -532,6 +533,7 @@ class Template extends Render } $parent = $this->_fenom->getRawTemplate()->load($tpl, false); $parent->blocks = & $this->blocks; + $parent->macros = & $this->macros; $parent->extended = $this->getName(); if (!$this->ext_stack) { $this->ext_stack[] = $this->getName(); @@ -1379,10 +1381,10 @@ class Template extends Render } } if ($recursive) { - $body = '$tpl->getMacro("' . $name . '")->__invoke($tpl);'; if($recursive instanceof Scope) { $recursive['recursive'] = true; } + return '$tpl->getMacro("' . $name . '")->__invoke('.Compiler::toArray($args).', $tpl);'; } else { $vars = $this->tmpVar(); return $vars . ' = $var; $var = ' . Compiler::toArray($args) . ';' . PHP_EOL . '?>' .