Skip to content

Commit

Permalink
Merge pull request #62 from Talesoft/varadic-fix
Browse files Browse the repository at this point in the history
Fixed a trailing semicolon in variadics (#61)
  • Loading branch information
TorbenKoehn committed Feb 3, 2016
2 parents 403a3c3 + b355159 commit 9edb8fa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
16 changes: 11 additions & 5 deletions Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ class Compiler
*/
private $_level;

/**
* Contains the current iterator ID to avoid name collisions.
*
* @var int
*/
private $_iteratorId;

/**
* Creates a new compiler instance.
*
Expand Down Expand Up @@ -441,6 +448,7 @@ public function compile($input, $path = null)
$this->_calledMixins = [];
$this->_blocks = [];
$this->_level = 0;
$this->_iteratorId = 0;

//Parse the input into an AST
$node = null;
Expand Down Expand Up @@ -1322,12 +1330,12 @@ protected function compileMixins()

if ($variadicIndex !== null) {

$args[$variadicName] = "array_slice(\$__arguments, $variadicIndex);";
$args[$variadicName] = "array_slice(\$__arguments, $variadicIndex)";
}

$phtml .= $this->createCode(
'$__mixins[\''.$name.'\'] = function(array $__arguments) use($__args, $__mixins) {
static $__defaults = '.$this->exportArray($args).';
$__defaults = '.$this->exportArray($args).';
$__arguments = array_replace($__defaults, $__arguments);
$__args = array_replace($__args, $__arguments);
extract($__args);
Expand Down Expand Up @@ -1613,8 +1621,6 @@ protected function compileWhen(Node $node)
protected function compileEach(Node $node)
{

static $id = 0;

$subject = $node->subject;

if ($this->isVariable($subject))
Expand All @@ -1625,7 +1631,7 @@ protected function compileEach(Node $node)
if ($node->keyName)
$as = "\${$node->keyName} => ".$as;

$var = '$__iterator'.($id++);
$var = '$__iterator'.($this->_iteratorId++);
$phtml = $this->createCode("$var = {$subject};").$this->newLine();
$phtml .= $this->indent().$this->createCode("foreach ($var as $as) {").$this->newLine();
$phtml .= $this->compileChildren($node->children).$this->newLine();
Expand Down
19 changes: 16 additions & 3 deletions Test/MixinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ public function setUp()
{

$this->_renderer = new Renderer([
'adapterOptions' => [
'path' => __DIR__.'/cache/mixins'
],
'cachePath' => __DIR__.'/cache/mixins',
'pretty' => false,
'paths' => [__DIR__.'/views/mixins']
]);
Expand Down Expand Up @@ -63,4 +61,19 @@ public function testIdAndClassForwarding()
'id-and-class-forwarding'
));
}

public function testVariadic()
{

$this->assertEquals('<h1>Test 1</h1><item name="Item 1" id="51"></item><item name="Item 2" id="52"></item><item name="Item 4" id="54"></item><h1>Test 2</h1><item name="Item 5" id="55"></item><item name="Item 6" id="56"></item><item name="Item 7" id="57"></item>', $this->_renderer->render('variadic', ['items' => [
['name' => 'Item 1', 'id' => 51],
['name' => 'Item 2', 'id' => 52],
['name' => 'Item 3', 'id' => 53],
['name' => 'Item 4', 'id' => 54],
['name' => 'Item 5', 'id' => 55],
['name' => 'Item 6', 'id' => 56],
['name' => 'Item 7', 'id' => 57],
['name' => 'Item 8', 'id' => 58],
]]));
}
}
11 changes: 11 additions & 0 deletions Test/views/mixins/variadic.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

mixin item(title, ...items)

h1= $title
each $item in $items
item(name=$item['name'], id=$item['id'])



+item('Test 1', $items[0], $items[1], $items[3])
+item('Test 2', $items[4], $items[5], $items[6])

0 comments on commit 9edb8fa

Please sign in to comment.