From b3480e820bd1ba97578928b03c33ba03b5f81d59 Mon Sep 17 00:00:00 2001 From: bzick Date: Sat, 23 Feb 2013 13:29:20 +0400 Subject: [PATCH] Add {import} + tests --- docs/tags/import.md | 6 +++--- docs/tags/macro.md | 14 ++++++-------- src/Aspect/Compiler.php | 24 ++++++++++++++++++------ src/Aspect/Template.php | 5 +++++ tests/cases/Aspect/MacrosTest.php | 30 ++++++++++++++++++++++++++---- 5 files changed, 58 insertions(+), 21 deletions(-) diff --git a/docs/tags/import.md b/docs/tags/import.md index 2d940d6..dc957e9 100644 --- a/docs/tags/import.md +++ b/docs/tags/import.md @@ -4,12 +4,12 @@ Tag {import} Import [macro](./macro.md) from another template ```smarty -{import 'page.macros.tpl'} +{import 'math.tpl'} ``` ```smarty -{import 'listing.tpl' as listing} +{import 'math.tpl' as math} ... -{listing.paginator current=5 count=100} +{math.plus x=5 y=100} ``` diff --git a/docs/tags/macro.md b/docs/tags/macro.md index 97320f8..439a3b6 100644 --- a/docs/tags/macro.md +++ b/docs/tags/macro.md @@ -1,20 +1,18 @@ Tag {macro} ============ -Declare +Declare macro ```smarty -declare macros - -{macro paginator(current, total, skip=true)} - ... paginator code ... +{macro plus(x, y, z=0)} + x + y + z = {$x + $y + $z} {/macro} ``` -Invoke +Invoke macro ```smarty -{paginator current=$page total=100} +{macro.plus x=$num y=100} ``` -Use tag [{import}](./import.md) for extending macros for another templates. \ No newline at end of file +Use tag [{import}](./import.md) for importing existing macroses into another template \ No newline at end of file diff --git a/src/Aspect/Compiler.php b/src/Aspect/Compiler.php index 0646a0a..40b0ede 100644 --- a/src/Aspect/Compiler.php +++ b/src/Aspect/Compiler.php @@ -658,11 +658,9 @@ class Compiler { throw new ImproperUseException("Cycle may contain only index attribute"); } else { return __CLASS__.'::cycle((array)'.$exp.', '.$p["index"].');'; - //$index = $p["index"]; } } else { return __CLASS__.'::cycle((array)'.$exp.', isset($i) ? $i++ : ($i = 0) );'; - //$index = false; } } @@ -679,11 +677,24 @@ class Compiler { if(!$name) { throw new ImproperUseException("Invalid usage tag {import}"); } + if($tokens->is(T_AS)) { + $alias = $tokens->next()->get(Tokenizer::MACRO_STRING); + $tokens->next(); + } else { + $alias = ""; + } $donor = $tpl->getStorage()->getRawTemplate()->load($name, true); - if(!empty($donor->_macros)) { - $tpl->_macros = array_merge($tpl->_macros, $donor->_macros); + if($donor->macros) { + foreach($donor->macros as $name => $macro) { + if($p = strpos($name, ".")) { + $name = substr($name, $p); + } + if($alias) { + $name = $alias.'.'.$name; + } + $tpl->macros[$name] = $macro; + } $tpl->addDepend($donor); - } return ''; } @@ -709,8 +720,9 @@ class Compiler { while($tokens->is(Tokenizer::MACRO_STRING)) { $scope["args"][] = $param = $tokens->getAndNext(); if($tokens->is('=')) { + $tokens->next(); if($tokens->is(T_CONSTANT_ENCAPSED_STRING, T_LNUMBER, T_DNUMBER) || $tokens->isSpecialVal()) { - $scope["defaults"][ $param ] = $tokens->current(); + $scope["defaults"][ $param ] = $tokens->getAndNext(); } else { throw new ImproperUseException("Macro parameters may have only scalar defaults"); } diff --git a/src/Aspect/Template.php b/src/Aspect/Template.php index 1285f62..1a8c067 100644 --- a/src/Aspect/Template.php +++ b/src/Aspect/Template.php @@ -35,6 +35,11 @@ class Template extends Render { * @var array of macros */ public $macros = array(); + + /** + * @var array of blocks + */ + public $blocks = array(); /** * Call stack * @var Scope[] diff --git a/tests/cases/Aspect/MacrosTest.php b/tests/cases/Aspect/MacrosTest.php index c155c1a..527000b 100644 --- a/tests/cases/Aspect/MacrosTest.php +++ b/tests/cases/Aspect/MacrosTest.php @@ -3,16 +3,38 @@ namespace Aspect; class MacrosTest extends TestCase { - public function testMacros() { - $tpl = $this->aspect->compileCode(' + public function setUp() { + parent::setUp(); + $this->tpl("math.tpl", ' {macro plus(x, y)} x + y = {$x + $y} {/macro} - Math: {macro.plus x=2 y=3} + {macro minus(x, y, z=0)} + x - y - z = {$x - $y - $z} + {/macro} + + Math: {macro.plus x=2 y=3}, {macro.minus x=10 y=4} '); + $this->tpl("import.tpl", ' + {import "math.tpl"} + {import "math.tpl" as math} + + Imp: {macro.plus x=1 y=2}, {math.minus x=6 y=2 z=1} + '); + } + + public function testMacros() { + $tpl = $this->aspect->compile('math.tpl'); + $this->assertStringStartsWith('x + y = ', trim($tpl->macros["plus"]["body"])); - $this->assertSame('Math: x + y = 5', Modifier::strip($tpl->fetch(array()), true)); + $this->assertSame('Math: x + y = 5 , x - y - z = 6', Modifier::strip($tpl->fetch(array()), true)); + } + + public function testImport() { + $tpl = $this->aspect->compile('import.tpl'); + + $this->assertSame('Imp: x + y = 3 , x - y - z = 3', Modifier::strip($tpl->fetch(array()), true)); } }