Add {import} + tests

This commit is contained in:
bzick 2013-02-23 13:29:20 +04:00
parent 703607102f
commit b3480e820b
5 changed files with 58 additions and 21 deletions

View File

@ -4,12 +4,12 @@ Tag {import}
Import [macro](./macro.md) from another template Import [macro](./macro.md) from another template
```smarty ```smarty
{import 'page.macros.tpl'} {import 'math.tpl'}
``` ```
```smarty ```smarty
{import 'listing.tpl' as listing} {import 'math.tpl' as math}
... ...
{listing.paginator current=5 count=100} {math.plus x=5 y=100}
``` ```

View File

@ -1,20 +1,18 @@
Tag {macro} Tag {macro}
============ ============
Declare Declare macro
```smarty ```smarty
declare macros {macro plus(x, y, z=0)}
x + y + z = {$x + $y + $z}
{macro paginator(current, total, skip=true)}
... paginator code ...
{/macro} {/macro}
``` ```
Invoke Invoke macro
```smarty ```smarty
{paginator current=$page total=100} {macro.plus x=$num y=100}
``` ```
Use tag [{import}](./import.md) for extending macros for another templates. Use tag [{import}](./import.md) for importing existing macroses into another template

View File

@ -658,11 +658,9 @@ class Compiler {
throw new ImproperUseException("Cycle may contain only index attribute"); throw new ImproperUseException("Cycle may contain only index attribute");
} else { } else {
return __CLASS__.'::cycle((array)'.$exp.', '.$p["index"].');'; return __CLASS__.'::cycle((array)'.$exp.', '.$p["index"].');';
//$index = $p["index"];
} }
} else { } else {
return __CLASS__.'::cycle((array)'.$exp.', isset($i) ? $i++ : ($i = 0) );'; return __CLASS__.'::cycle((array)'.$exp.', isset($i) ? $i++ : ($i = 0) );';
//$index = false;
} }
} }
@ -679,11 +677,24 @@ class Compiler {
if(!$name) { if(!$name) {
throw new ImproperUseException("Invalid usage tag {import}"); 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); $donor = $tpl->getStorage()->getRawTemplate()->load($name, true);
if(!empty($donor->_macros)) { if($donor->macros) {
$tpl->_macros = array_merge($tpl->_macros, $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); $tpl->addDepend($donor);
} }
return ''; return '';
} }
@ -709,8 +720,9 @@ class Compiler {
while($tokens->is(Tokenizer::MACRO_STRING)) { while($tokens->is(Tokenizer::MACRO_STRING)) {
$scope["args"][] = $param = $tokens->getAndNext(); $scope["args"][] = $param = $tokens->getAndNext();
if($tokens->is('=')) { if($tokens->is('=')) {
$tokens->next();
if($tokens->is(T_CONSTANT_ENCAPSED_STRING, T_LNUMBER, T_DNUMBER) || $tokens->isSpecialVal()) { if($tokens->is(T_CONSTANT_ENCAPSED_STRING, T_LNUMBER, T_DNUMBER) || $tokens->isSpecialVal()) {
$scope["defaults"][ $param ] = $tokens->current(); $scope["defaults"][ $param ] = $tokens->getAndNext();
} else { } else {
throw new ImproperUseException("Macro parameters may have only scalar defaults"); throw new ImproperUseException("Macro parameters may have only scalar defaults");
} }

View File

@ -35,6 +35,11 @@ class Template extends Render {
* @var array of macros * @var array of macros
*/ */
public $macros = array(); public $macros = array();
/**
* @var array of blocks
*/
public $blocks = array();
/** /**
* Call stack * Call stack
* @var Scope[] * @var Scope[]

View File

@ -3,16 +3,38 @@ namespace Aspect;
class MacrosTest extends TestCase { class MacrosTest extends TestCase {
public function testMacros() { public function setUp() {
$tpl = $this->aspect->compileCode(' parent::setUp();
$this->tpl("math.tpl", '
{macro plus(x, y)} {macro plus(x, y)}
x + y = {$x + $y} x + y = {$x + $y}
{/macro} {/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->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));
} }
} }