diff --git a/Compiler.php b/Compiler.php index 9228734..7b8be11 100644 --- a/Compiler.php +++ b/Compiler.php @@ -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. * @@ -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; @@ -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); @@ -1613,8 +1621,6 @@ protected function compileWhen(Node $node) protected function compileEach(Node $node) { - static $id = 0; - $subject = $node->subject; if ($this->isVariable($subject)) @@ -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(); diff --git a/Test/MixinTest.php b/Test/MixinTest.php index 8699b84..9101e99 100644 --- a/Test/MixinTest.php +++ b/Test/MixinTest.php @@ -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'] ]); @@ -63,4 +61,19 @@ public function testIdAndClassForwarding() 'id-and-class-forwarding' )); } + + public function testVariadic() + { + + $this->assertEquals('

Test 1

Test 2

', $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], + ]])); + } } \ No newline at end of file diff --git a/Test/views/mixins/variadic.jade b/Test/views/mixins/variadic.jade new file mode 100644 index 0000000..8c05d35 --- /dev/null +++ b/Test/views/mixins/variadic.jade @@ -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])