This commit is contained in:
bzick 2015-02-19 17:04:15 +03:00
parent 4dbbc81785
commit 8f3c94a7a1
3 changed files with 35 additions and 7 deletions

View File

@ -819,11 +819,16 @@ class Template extends Render
if ($tokens->isSpecialVal()) { if ($tokens->isSpecialVal()) {
return $unary . $tokens->getAndNext(); return $unary . $tokens->getAndNext();
} elseif ($tokens->isNext("(") && !$tokens->getWhitespace()) { } elseif ($tokens->isNext("(") && !$tokens->getWhitespace()) {
$func = $this->_fenom->getModifier($tokens->current(), $this); $func = $this->_fenom->getModifier($modifier = $tokens->current(), $this);
if (!$func) { if (!$func) {
throw new \Exception("Function " . $tokens->getAndNext() . " not found"); throw new \Exception("Function " . $tokens->getAndNext() . " not found");
} }
return $unary . $this->parseChain($tokens, $func . $this->parseArgs($tokens->next())); if (!is_string($func)) { // dynamic modifier
$call = 'call_user_func_array($tpl->getStorage()->getModifier("' . $modifier . '"), array'.$this->parseArgs($tokens->next()).')'; // @todo optimize
} else {
$call = $func . $this->parseArgs($tokens->next());
}
return $unary . $this->parseChain($tokens, $call);
} elseif ($tokens->isNext(T_NS_SEPARATOR, T_DOUBLE_COLON)) { } elseif ($tokens->isNext(T_NS_SEPARATOR, T_DOUBLE_COLON)) {
$method = $this->parseStatic($tokens); $method = $this->parseStatic($tokens);
$args = $this->parseArgs($tokens); $args = $this->parseArgs($tokens);
@ -1389,7 +1394,7 @@ class Template extends Render
* (1 + 2.3, 'string', $var, [2,4]) * (1 + 2.3, 'string', $var, [2,4])
* *
* @param Tokenizer $tokens * @param Tokenizer $tokens
* @throws TokenizeException * @param bool $as_string
* @return string * @return string
*/ */
public function parseArgs(Tokenizer $tokens) public function parseArgs(Tokenizer $tokens)

View File

@ -13,8 +13,9 @@ class SandboxTest extends TestCase {
// return '<?php ' . $tag->cutContent(); // return '<?php ' . $tag->cutContent();
// }); // });
// $this->tpl('welcome.tpl', '{$a}'); // $this->tpl('welcome.tpl', '{$a}');
// $this->fenom->addModifier('min', function () {});
// try { // try {
// var_dump($this->fenom->compileCode('{for $i=0 to 3} {/for}')->getBody()); // var_dump($this->fenom->compileCode('{time() + min(1, 10)}')->getBody());
// } catch (\Exception $e) { // } catch (\Exception $e) {
// print_r($e->getMessage() . "\n" . $e->getTraceAsString()); // print_r($e->getMessage() . "\n" . $e->getTraceAsString());
// while ($e->getPrevious()) { // while ($e->getPrevious()) {

View File

@ -119,10 +119,32 @@ class FenomTest extends \Fenom\TestCase
public function testSetModifier() public function testSetModifier()
{ {
$this->fenom->addModifier("mymod", "myMod"); $this->fenom->addModifier("mymod", "myMod");
$this->tpl('custom.tpl', 'Custom modifier {$a|mymod}'); $this->assertRender(
$this->assertSame( 'Custom modifier {$a|mymod}',
"Custom modifier (myMod)Custom(/myMod)", "Custom modifier (myMod)Custom(/myMod)",
$this->fenom->fetch('custom.tpl', array("a" => "Custom")) array("a" => "Custom")
);
$this->assertRender(
'Custom modifier {mymod($a)}',
"Custom modifier (myMod)Custom(/myMod)",
array("a" => "Custom")
);
}
public function testSetModifierClosure()
{
$this->fenom->addModifier("mymod", function ($value) {
return "(myMod)$value(/myMod)";
});
$this->assertRender(
'Custom modifier {$a|mymod}',
"Custom modifier (myMod)Custom(/myMod)",
array("a" => "Custom")
);
$this->assertRender(
'Custom modifier {mymod($a)}',
"Custom modifier (myMod)Custom(/myMod)",
array("a" => "Custom")
); );
} }