mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Add {import} + tests
This commit is contained in:
parent
703607102f
commit
b3480e820b
@ -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}
|
||||
```
|
||||
|
||||
|
@ -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.
|
||||
Use tag [{import}](./import.md) for importing existing macroses into another template
|
@ -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");
|
||||
}
|
||||
|
@ -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[]
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user