From cb803f5d9cc544f726a80c6297c68fd003b26a50 Mon Sep 17 00:00:00 2001 From: bzick Date: Thu, 22 Aug 2013 00:03:20 +0400 Subject: [PATCH] Add 'how it work' --- CHANGELOG.md | 8 +++++++ docs/{develop.md => dev/readme.md} | 4 +++- src/Fenom.php | 2 +- src/Fenom/Render.php | 10 +++++++++ src/Fenom/Template.php | 35 ++++++++++++++++++------------ tests/cases/Fenom/MacrosTest.php | 21 ++++++++++++++---- 6 files changed, 60 insertions(+), 20 deletions(-) rename docs/{develop.md => dev/readme.md} (57%) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4b4e9a..a0f3856 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ CHANGELOG ========= +## 1.3.0 + +- Feature #41: Add system variable `$`. +- Fix bug when recursive macros doesn't work in Template +- Recognize variable parser +- Tests++ +- Docs-- + ### 1.2.2 (2013-08-07) - Fix bug in setOptions method diff --git a/docs/develop.md b/docs/dev/readme.md similarity index 57% rename from docs/develop.md rename to docs/dev/readme.md index c84934c..3199ae1 100644 --- a/docs/develop.md +++ b/docs/dev/readme.md @@ -3,4 +3,6 @@ Develop If you want to discuss the enhancement of the Fenom, create an issue on Github or submit a pull request. -For questions: a.cobest@gmail.com (English, Russian languages) \ No newline at end of file +For questions: a.cobest@gmail.com (English, Russian languages) + +[./git.md](Git conversation) \ No newline at end of file diff --git a/src/Fenom.php b/src/Fenom.php index 6080a83..dfb97f8 100644 --- a/src/Fenom.php +++ b/src/Fenom.php @@ -17,7 +17,7 @@ use Fenom\Template; */ class Fenom { - const VERSION = '1.2'; + const VERSION = '1.3'; /* Actions */ const INLINE_COMPILER = 1; diff --git a/src/Fenom/Render.php b/src/Fenom/Render.php index 36f4a8c..b9d11c3 100644 --- a/src/Fenom/Render.php +++ b/src/Fenom/Render.php @@ -9,6 +9,7 @@ */ namespace Fenom; use Fenom; +use Symfony\Component\Yaml\Exception\RuntimeException; /** * Primitive template @@ -69,6 +70,11 @@ class Render extends \ArrayObject */ protected $_provider; + /** + * @var \Closure[] + */ + protected $_macros; + /** * @param Fenom $fenom * @param callable $code template body @@ -192,6 +198,10 @@ class Render extends \ArrayObject */ public function getMacro($name) { + if(empty($this->_macros[$name])) { + var_dump($this->_macros[$name]); exit; + throw new RuntimeException('macro not found'); + } return $this->_macros[$name]; } diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index 3057d63..7d3a21b 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -397,18 +397,6 @@ class Template extends Render */ public function getTemplateCode() { - - if ($this->macros) { - $macros = array(); - foreach ($this->macros as $m) { - if ($m["recursive"]) { - $macros[] = "\t\t'" . $m["name"] . "' => function (\$tpl) {\n?>" . $m["body"] . "_before ? $this->_before . "\n" : ""; return "_name . "' compiled at " . date('Y-m-d H:i:s') . " */\n" . @@ -420,10 +408,29 @@ class Template extends Render "\t'base_name' => " . var_export($this->_base_name, true) . ",\n" . "\t'time' => {$this->_time},\n" . "\t'depends' => " . var_export($this->_base_name, true) . ",\n" . - "\t'macros' => array({$macros}), + "\t'macros' => " . $this->_getMacrosArray() . ",\n ));\n"; } + /** + * Make array with macros code + * @return string + */ + private function _getMacrosArray() + { + if ($this->macros) { + $macros = array(); + foreach ($this->macros as $m) { + if ($m["recursive"]) { + $macros[] = "\t\t'" . $m["name"] . "' => function (\$tpl) {\n?>" . $m["body"] . "_code) { // evaluate template's code - eval("\$this->_code = " . $this->_getClosureSource() . ";"); + eval("\$this->_code = " . $this->_getClosureSource() . ";\n\$this->_macros = ".$this->_getMacrosArray() .';'); if (!$this->_code) { throw new CompileException("Fatal error while creating the template"); } diff --git a/tests/cases/Fenom/MacrosTest.php b/tests/cases/Fenom/MacrosTest.php index 7b5c32c..b675e3a 100644 --- a/tests/cases/Fenom/MacrosTest.php +++ b/tests/cases/Fenom/MacrosTest.php @@ -51,16 +51,21 @@ class MacrosTest extends TestCase {/if} {/macro} - {macro.factorial num=10}'); + XXXX: {macro.factorial num=10}'); + + $this->tpl("macro_recursive_import.tpl", ' + {import "macro_recursive.tpl" as math} + + YYYY: {math.factorial num=10}'); } - public function _testSandbox() + public function testSandbox() { try { - $this->fenom->compile("macro_recursive.tpl"); +// $this->fenom->compile("macro_recursive.tpl")->display([]); // $this->fenom->flush(); // var_dump($this->fenom->fetch("macro_recursive.tpl", [])); - var_dump( $this->fenom->compile("macro_recursive.tpl")->getTemplateCode()); + var_dump( $this->fenom->compile("macro_recursive_import.tpl")->getTemplateCode([])); } catch (\Exception $e) { var_dump($e->getMessage() . ": " . $e->getTraceAsString()); } @@ -107,4 +112,12 @@ class MacrosTest extends TestCase $tpl = $this->fenom->getTemplate('macro_recursive.tpl'); $this->assertSame("10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10", Modifier::strip($tpl->fetch(array()), true)); } + + public function testImportRecursive() + { + $this->fenom->compile('macro_recursive_import.tpl'); + $this->fenom->flush(); + $tpl = $this->fenom->getTemplate('macro_recursive.tpl'); + $this->assertSame("10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10", Modifier::strip($tpl->fetch(array()), true)); + } }