From d8c4ea69945310916127bd73903232262397a369 Mon Sep 17 00:00:00 2001 From: bzick Date: Mon, 12 May 2014 00:36:50 +0400 Subject: [PATCH] Done :ignore option, improve ignore mechanism --- src/Fenom.php | 5 +++ src/Fenom/Compiler.php | 10 +++++ src/Fenom/Tag.php | 10 +++++ src/Fenom/Template.php | 62 +++++++++++------------------- tests/cases/Fenom/TemplateTest.php | 5 ++- 5 files changed, 52 insertions(+), 40 deletions(-) diff --git a/src/Fenom.php b/src/Fenom.php index 5f3b78a..1e94bce 100644 --- a/src/Fenom.php +++ b/src/Fenom.php @@ -277,6 +277,11 @@ class Fenom 'type' => self::BLOCK_COMPILER, 'open' => 'Fenom\Compiler::stripOpen', 'close' => 'Fenom\Compiler::nope' + ), + 'ignore' => array( + 'type' => self::BLOCK_COMPILER, + 'open' => 'Fenom\Compiler::ignoreOpen', + 'close' => 'Fenom\Compiler::nope' ) ); diff --git a/src/Fenom/Compiler.php b/src/Fenom/Compiler.php index 91c987a..c6a2d74 100644 --- a/src/Fenom/Compiler.php +++ b/src/Fenom/Compiler.php @@ -986,4 +986,14 @@ class Compiler $tokens->next(); $tag->setOption(\Fenom::AUTO_STRIP, $expected); } + + /** + * Tag {ignore} + * @param Tokenizer $tokens + * @param Tag $tag + */ + public static function ignoreOpen(Tokenizer $tokens, Tag $tag) + { + $tag->tpl->ignore('ignore'); + } } diff --git a/src/Fenom/Tag.php b/src/Fenom/Tag.php index e71ffdd..1663613 100644 --- a/src/Fenom/Tag.php +++ b/src/Fenom/Tag.php @@ -286,4 +286,14 @@ class Tag extends \ArrayObject { $this->setOption(\Fenom::AUTO_STRIP, true); } + + /** + * Enable ignore for body of the tag + */ + public function optIgnore() + { + if(!$this->isClosed()) { + $this->tpl->ignore($this->name); + } + } } \ No newline at end of file diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index ed6d805..a2aca54 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -96,7 +96,7 @@ class Template extends Render private $_line = 1; private $_post = array(); /** - * @var bool + * @var bool|string */ private $_ignore = false; @@ -243,24 +243,22 @@ class Template extends Render $this->_ignore = false; } else { // still ignore $this->_appendText($tag); + continue; } + } + + if ($this->_tag_filters) { + foreach ($this->_tag_filters as $filter) { + $_tag = call_user_func($filter, $_tag, $this); + } + } + $tokens = new Tokenizer($_tag); // tokenize the tag + if ($tokens->isIncomplete()) { // all strings finished? + $need_more = true; } else { - if ($this->_tag_filters) { - foreach ($this->_tag_filters as $filter) { - $_tag = call_user_func($filter, $_tag, $this); - } - } - $tokens = new Tokenizer($_tag); // tokenize the tag - if ($tokens->isIncomplete()) { // all strings finished? - $need_more = true; - } else { - $this->_appendCode($this->parseTag($tokens), $tag); // start the tag lexer - if ($tokens->key()) { // if tokenizer have tokens - throws exceptions - throw new CompileException("Unexpected token '" . $tokens->current() . "' in {$this} line {$this->_line}, near '{" . $tokens->getSnippetAsString( - 0, - 0 - ) . "' <- there", 0, E_ERROR, $this->_name, $this->_line); - } + $this->_appendCode($this->parseTag($tokens), $tag); // start the tag lexer + if ($tokens->key()) { // if tokenizer have tokens - throws exceptions + throw new CompileException("Unexpected token '" . $tokens->current() . "' in {$this} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line); } } } while ($need_more); @@ -381,6 +379,10 @@ class Template extends Render } } + public function ignore($tag_name) { + $this->_ignore = $tag_name; + } + /** * @param callable[] $cb */ @@ -552,13 +554,7 @@ class Template extends Render { try { if ($tokens->is(Tokenizer::MACRO_STRING)) { - if ($tokens->current() === "ignore") { - $this->_ignore = "ignore"; - $tokens->next(); - return ''; - } else { - return $this->parseAct($tokens); - } + return $this->parseAct($tokens); } elseif ($tokens->is('/')) { return $this->parseEndTag($tokens); } else { @@ -567,15 +563,9 @@ class Template extends Render } catch (InvalidUsageException $e) { throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}", 0, E_ERROR, $this->_name, $this->_line, $e); } catch (\LogicException $e) { - throw new SecurityException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString( - 0, - 0 - ) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e); + throw new SecurityException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e); } catch (\Exception $e) { - throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString( - 0, - 0 - ) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e); + throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e); } } @@ -934,10 +924,7 @@ class Template extends Render switch ($key) { case 'const': $tokens->need('.')->next(); - $var = $this->parseName($tokens); - if (!defined($var)) { - $var = '@constant(' . var_export($var, true) . ')'; - } + $var = '@constant(' . var_export($this->parseName($tokens), true) . ')'; break; case 'version': $var = '\Fenom::VERSION'; @@ -1156,9 +1143,6 @@ class Template extends Render case '"': return $this->parseQuote($tokens); break; - case '$': - $tokens->next()->need('.')->next()->need(T_CONST)->next(); - return @constant($this->parseName($tokens)); default: throw new UnexpectedTokenException($tokens); } diff --git a/tests/cases/Fenom/TemplateTest.php b/tests/cases/Fenom/TemplateTest.php index 86da05f..02a6095 100644 --- a/tests/cases/Fenom/TemplateTest.php +++ b/tests/cases/Fenom/TemplateTest.php @@ -62,6 +62,7 @@ class TemplateTest extends TestCase array('hello, {$b."$c"}!', $b, 'hello, Username!'), array('hello, {$b."{$c}"}!', $b, 'hello, Username!'), array('hello, {$b[ "{$c}" ]}!', $b, 'hello, Username!'), + array('hello, {$b[ ("{$c}") ]}!', $b, 'hello, Username!'), array('hello, {$b[ "mcp" ]}!', $b, 'hello, Master!'), array('hello, {$b[ "m{$c}p" ]}!', $b, 'hello, Master!'), array('hello, {$b."m{$c}p"}!', $b, 'hello, Master!'), @@ -850,7 +851,8 @@ class TemplateTest extends TestCase $a, 'literal: { $a} end' ), - array('{if 0}none{/if}literal: function () { return 1; } end', $a, 'literal: function () { return 1; } end') + array('{if 0}none{/if}literal: function () { return 1; } end', $a, 'literal: function () { return 1; } end'), + array('{if:ignore 1}literal: {$a} end{/if}', $a, 'literal: {$a} end'), ); } @@ -1454,6 +1456,7 @@ class TemplateTest extends TestCase } /** + * @group testIgnores * @dataProvider providerIgnores */ public function testIgnores($code, $vars, $result)