diff --git a/src/Fenom/Compiler.php b/src/Fenom/Compiler.php index e1edd38..64fd225 100644 --- a/src/Fenom/Compiler.php +++ b/src/Fenom/Compiler.php @@ -635,7 +635,6 @@ class Compiler /** * Standard close tag {/...} * - * @static * @return string */ public static function stdClose() @@ -646,20 +645,23 @@ class Compiler /** * Standard function parser * - * @static * @param Tokenizer $tokens * @param Tag $tag * @return string */ public static function stdFuncParser(Tokenizer $tokens, Tag $tag) { - return $tag->out($tag->callback . "(" . self::toArray($tag->tpl->parseParams($tokens)) . ', $tpl)'); + if(is_string($tag->callback)) { + return $tag->out($tag->callback . "(" . self::toArray($tag->tpl->parseParams($tokens)) . ', $tpl)'); + } else { + return '$info = $tpl->getStorage()->getTag('.var_export($tag->name, true).');'.PHP_EOL. + $tag->out('call_user_func($info["function"], '.self::toArray($tag->tpl->parseParams($tokens)).', $tpl)'); + } } /** * Smart function parser * - * @static * @param Tokenizer $tokens * @param Tag $tag * @return string @@ -689,7 +691,6 @@ class Compiler /** * Standard function open tag parser * - * @static * @param Tokenizer $tokens * @param Tag $tag * @return string @@ -704,7 +705,6 @@ class Compiler /** * Standard function close tag parser * - * @static * @param Tokenizer $tokens * @param Tag $tag * @return string @@ -712,7 +712,12 @@ class Compiler public static function stdFuncClose($tokens, Tag $tag) { $tag->restore(\Fenom::AUTO_ESCAPE); - return $tag->out($tag->callback . '(' . $tag["params"] . ', ob_get_clean(), $tpl)'); + if(is_string($tag->callback)) { + return $tag->out($tag->callback . "(" . $tag["params"] . ', ob_get_clean(), $tpl)'); + } else { + return '$info = $tpl->getStorage()->getTag('.var_export($tag->name, true).');'.PHP_EOL. + $tag->out('call_user_func($info["function"], ' . $tag["params"] . ', ob_get_clean(), $tpl)'); + } } /** diff --git a/tests/cases/FenomTest.php b/tests/cases/FenomTest.php index e3aac2f..9ce3802 100644 --- a/tests/cases/FenomTest.php +++ b/tests/cases/FenomTest.php @@ -130,13 +130,33 @@ class FenomTest extends \Fenom\TestCase */ public function testSetFunctions() { + $test = $this; $this->fenom->setOptions(Fenom::FORCE_COMPILE); $this->fenom->addFunction("myfunc", "myFunc"); + $this->fenom->addFunction("myfunc2", function ($args, $tpl) use ($test) { + $test->assertInstanceOf('Fenom\Render', $tpl); + $test->assertSame(array( + "name" => "foo" + ), $args); + return "MyFunc2:".$args['name']; + }); $this->fenom->addBlockFunction("myblockfunc", "myBlockFunc"); + $this->fenom->addBlockFunction("myblockfunc2", function ($args, $content, $tpl) use ($test) { + $test->assertInstanceOf('Fenom\Render', $tpl); + $test->assertSame(array( + "name" => "foo" + ), $args); + $this->assertSame(' this block1 ', $content); + return "Block2:" . $args["name"] . ':' . trim($content) . ':Block'; + }); $this->tpl('custom.tpl', 'Custom function {myfunc name="foo"}'); $this->assertSame("Custom function MyFunc:foo", $this->fenom->fetch('custom.tpl', array())); + $this->tpl('custom.tpl', 'Custom function {myfunc2 name="foo"}'); + $this->assertSame("Custom function MyFunc2:foo", $this->fenom->fetch('custom.tpl', array())); $this->tpl('custom.tpl', 'Custom function {myblockfunc name="foo"} this block1 {/myblockfunc}'); $this->assertSame("Custom function Block:foo:this block1:Block", $this->fenom->fetch('custom.tpl', array())); + $this->tpl('custom.tpl', 'Custom function {myblockfunc2 name="foo"} this block1 {/myblockfunc2}'); + $this->assertSame("Custom function Block2:foo:this block1:Block", $this->fenom->fetch('custom.tpl', array())); } public function testSetCompilers() @@ -253,7 +273,7 @@ class FenomTest extends \Fenom\TestCase ); } - public function testAddFunctions() + public function testAddAllowedFunctions() { $this->fenom->setOptions(Fenom::DENY_NATIVE_FUNCS); $this->assertFalse($this->fenom->isAllowedFunction('substr')); @@ -261,6 +281,9 @@ class FenomTest extends \Fenom\TestCase $this->assertTrue($this->fenom->isAllowedFunction('substr')); } + + + /** * @requires function php_gte_54 * @group pipe