diff --git a/.gitignore b/.gitignore index 735129a..53f7e1a 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,6 @@ tests/resources/template/* !.gitkeep benchmark/compile/* benchmark/templates/inheritance/smarty -benchmark/templates/inheritance/twig \ No newline at end of file +benchmark/templates/inheritance/twig +benchmark/sandbox/compiled/* +!.gitkeep \ No newline at end of file diff --git a/benchmark/sandbox/compiled/.gitkeep b/benchmark/sandbox/compiled/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/benchmark/sandbox/fenom.php b/benchmark/sandbox/fenom.php index b723509..ef220e5 100644 --- a/benchmark/sandbox/fenom.php +++ b/benchmark/sandbox/fenom.php @@ -2,6 +2,10 @@ require_once __DIR__.'/../../vendor/autoload.php'; -$t = new Fenom\Tokenizer('some "asd {$$ddd} dfg" some'); +$fenom = Fenom::factory(__DIR__.'/templates', __DIR__.'/compiled', Fenom::FORCE_COMPILE); -var_dump($t->tokens); \ No newline at end of file +$fenom->display("greeting.tpl", array( + "user" => array( + "name" => "Ivka" + ) +)); \ No newline at end of file diff --git a/benchmark/sandbox/templates/greeting.tpl b/benchmark/sandbox/templates/greeting.tpl new file mode 100644 index 0000000..0b97ab3 --- /dev/null +++ b/benchmark/sandbox/templates/greeting.tpl @@ -0,0 +1,8 @@ +Greeting, +{if $user} + {$user.name}! +{else} + anonymous? +{/if} + +3 \ No newline at end of file diff --git a/src/Fenom.php b/src/Fenom.php index dfb97f8..02a10a7 100644 --- a/src/Fenom.php +++ b/src/Fenom.php @@ -757,12 +757,15 @@ class Fenom protected function _load($tpl, $opts) { $file_name = $this->_getCacheName($tpl, $opts); - if (!is_file($this->_compile_dir . "/" . $file_name)) { - return $this->compile($tpl, true, $opts); - } else { + if (is_file($this->_compile_dir . "/" . $file_name)) { $fenom = $this; - return include($this->_compile_dir . "/" . $file_name); + $_tpl = include($this->_compile_dir . "/" . $file_name); + /* @var Fenom\Render $tpl */ + if($_tpl->isValid()) { + return $_tpl; + } } + return $this->compile($tpl, true, $opts); } /** diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index 7d3a21b..033e32f 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -99,7 +99,7 @@ class Template extends Render private $_filters = array(); - private static $_checkers = array( + protected static $_tests = array( 'integer' => 'is_int(%s)', 'int' => 'is_int(%s)', 'float' => 'is_float(%s)', @@ -223,6 +223,10 @@ class Template extends Render unset($comment); // cleanup break; default: +// var_dump($this->_src[$pos]); + if($this->_src[$pos] === "\n") { + $pos++; + } $this->_appendText(substr($this->_src, $pos, $start - $pos)); $end = $start + 1; do { @@ -260,6 +264,9 @@ class Template extends Render } gc_collect_cycles(); + if($end < strlen($this->_src) && $this->_src[$end + 1] === "\n") { + $end++; + } $this->_appendText(substr($this->_src, $end ? $end + 1 : 0)); // append tail of the template if ($this->_stack) { $_names = array(); @@ -466,6 +473,7 @@ class Template extends Render */ public function addDepend(Render $tpl) { +// var_dump($tpl->getScm(),"$tpl", (new \Exception())->getTraceAsString() ); $this->_depends[$tpl->getScm()][$tpl->getName()] = $tpl->getTime(); } @@ -1007,7 +1015,7 @@ class Template extends Render /** * Parse 'is' and 'is not' operators - * @see $_checkers + * @see _tests * @param Tokenizer $tokens * @param string $value * @param bool $variable @@ -1030,10 +1038,10 @@ class Template extends Render if (!$variable && ($action == "set" || $action == "empty")) { $action = "_$action"; $tokens->next(); - return $invert . sprintf(self::$_checkers[$action], $value); - } elseif (isset(self::$_checkers[$action])) { + return $invert . sprintf(self::$_tests[$action], $value); + } elseif (isset(self::$_tests[$action])) { $tokens->next(); - return $invert . sprintf(self::$_checkers[$action], $value); + return $invert . sprintf(self::$_tests[$action], $value); } elseif ($tokens->isSpecialVal()) { $tokens->next(); return '(' . $value . ' ' . $equal . '= ' . $action . ')'; diff --git a/tests/cases/Fenom/MacrosTest.php b/tests/cases/Fenom/MacrosTest.php index fee632e..06551ef 100644 --- a/tests/cases/Fenom/MacrosTest.php +++ b/tests/cases/Fenom/MacrosTest.php @@ -65,7 +65,8 @@ class MacrosTest extends TestCase // $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_import.tpl")->getTemplateCode([])); + var_dump( $this->fenom->compile("macro_recursive_import.tpl")->display([])); + var_dump( $this->fenom->display("macro_recursive_import.tpl", [])); } catch (\Exception $e) { var_dump($e->getMessage() . ": " . $e->getTraceAsString()); }