Done :ignore option, improve ignore mechanism

This commit is contained in:
bzick 2014-05-12 00:36:50 +04:00
parent 58d118496c
commit d8c4ea6994
5 changed files with 52 additions and 40 deletions

View File

@ -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'
)
);

View File

@ -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');
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}

View File

@ -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)