From dec06c888eb4cefbe5942c749bc9c003e782496d Mon Sep 17 00:00:00 2001 From: bzick Date: Sun, 8 May 2016 18:26:01 +0300 Subject: [PATCH] Done #228 --- docs/en/tags/do.md | 9 +++++++++ docs/ru/tags/do.md | 9 +++++++++ sandbox/fenom.php | 5 +---- src/Fenom.php | 4 ++++ src/Fenom/Compiler.php | 5 +++++ src/Fenom/RangeIterator.php | 3 ++- tests/cases/Fenom/TemplateTest.php | 24 +++++++++++++++++++++++- 7 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 docs/en/tags/do.md create mode 100644 docs/ru/tags/do.md diff --git a/docs/en/tags/do.md b/docs/en/tags/do.md new file mode 100644 index 0000000..c10f273 --- /dev/null +++ b/docs/en/tags/do.md @@ -0,0 +1,9 @@ +Tag {do} +======== + +Evaluates any expression and doesn't print anything + +```smarty +{do $count++} +{do $object->method()} +``` \ No newline at end of file diff --git a/docs/ru/tags/do.md b/docs/ru/tags/do.md new file mode 100644 index 0000000..4e65853 --- /dev/null +++ b/docs/ru/tags/do.md @@ -0,0 +1,9 @@ +Тег {do} +======== + +Выполнить произвольноые выражение без вывода результата на экран + +```smarty +{do $count++} +{do $object->method()} +``` \ No newline at end of file diff --git a/sandbox/fenom.php b/sandbox/fenom.php index 1fe4b8a..3f3c9fd 100644 --- a/sandbox/fenom.php +++ b/sandbox/fenom.php @@ -7,10 +7,7 @@ require_once __DIR__.'/../tests/tools.php'; $fenom = Fenom::factory(__DIR__.'/templates', __DIR__.'/../tests/resources/compile'); $fenom->setOptions(Fenom::AUTO_RELOAD); -$fenom->addModifier('firstimg', function ($img) { - return $img; -}); -var_dump($fenom->compileCode('{foreach $list as $k => $e last=$l first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {if $l}last{/if} {/foreach}')->getTemplateCode()); +var_dump($fenom->compileCode('{do $object->method()}')->getTemplateCode()); //var_dump($fenom->compile("bug158/main.tpl", [])->getTemplateCode()); //var_dump($fenom->display("bug158/main.tpl", [])); // $fenom->getTemplate("problem.tpl"); \ No newline at end of file diff --git a/src/Fenom.php b/src/Fenom.php index f7e5862..f6b3432 100644 --- a/src/Fenom.php +++ b/src/Fenom.php @@ -272,6 +272,10 @@ class Fenom 'open' => 'Fenom\Compiler::setOpen', 'close' => 'Fenom\Compiler::setClose' ), + 'do' => array( // {do ...} + 'type' => self::INLINE_COMPILER, + 'parser' => 'Fenom\Compiler::tagDo' + ), 'block' => array( // {block ...} {parent} {/block} 'type' => self::BLOCK_COMPILER, 'open' => 'Fenom\Compiler::tagBlockOpen', diff --git a/src/Fenom/Compiler.php b/src/Fenom/Compiler.php index d057f68..a598122 100644 --- a/src/Fenom/Compiler.php +++ b/src/Fenom/Compiler.php @@ -813,6 +813,11 @@ class Compiler return $scope["name"] . '=' . $scope["value"] . ';'; } + public static function tagDo($tokens, Tag $scope) + { + return $scope->tpl->parseExpr($tokens).';'; + } + /** * @param Tokenizer $tokens diff --git a/src/Fenom/RangeIterator.php b/src/Fenom/RangeIterator.php index 8ca0981..2d5cfb1 100644 --- a/src/Fenom/RangeIterator.php +++ b/src/Fenom/RangeIterator.php @@ -23,7 +23,8 @@ class RangeIterator implements \Iterator, \Countable * @param int $step * @return $this */ - public function setStep($step) { + public function setStep($step) + { if($step > 0) { $this->current = min($this->min, $this->max); } elseif($step < 0) { diff --git a/tests/cases/Fenom/TemplateTest.php b/tests/cases/Fenom/TemplateTest.php index 7d3cef4..31247d3 100644 --- a/tests/cases/Fenom/TemplateTest.php +++ b/tests/cases/Fenom/TemplateTest.php @@ -111,6 +111,18 @@ class TemplateTest extends TestCase ); } + public static function providerDo() { + $vars = array( + "c" => 4, + "world" => new Helper('world') + ); + return array( + array('{do "nope"}', $vars, ""), + array('{do $c++} -> {$c}', $vars, "-> 5"), + array('{do $world->chunk(1)}', $vars, ""), + ); + } + public static function providerVarsInvalid() { @@ -1154,6 +1166,15 @@ class TemplateTest extends TestCase $this->exec($code, $vars, $result); } + /** + * @dataProvider providerDo + */ + public function testDo($code, $vars, $result) + { + $this->exec($code, $vars, $result); + } + + /** * @dataProvider providerVarsInvalid */ @@ -1587,6 +1608,8 @@ class TemplateTest extends TestCase { return array( array('{set $a=1..3}', "1,2,3,"), +// array('{set $a=0..0}', ""), +// array('{set $a=1..1}', ""), // array('{set $a="a".."f"}', "a,b,c,d,e,f,"), // array('{set $a=1.."f"}', "1,0,"), // array('{set $a="a"..2}', "0,1,2,"), @@ -1618,7 +1641,6 @@ class TemplateTest extends TestCase /** * @dataProvider providerRange * @group testRange - * @group dev * @param string $code * @param string $result */