Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ private function handleFor( DOMNode $node, array $data ) {
if ( $node->hasAttribute( 'v-for' ) ) {
list( $itemName, $listName ) = explode( ' in ', $node->getAttribute( 'v-for' ) );
$node->removeAttribute( 'v-for' );
$node->removeAttribute( ':key' );

foreach ( $data[$listName] as $item ) {
foreach ( $this->app->evaluateExpression( $listName, $data ) as $item ) {
$newNode = $node->cloneNode( true );
$node->parentNode->insertBefore( $newNode, $node );
$this->handleNode( $newNode, array_merge( $data, [ $itemName => $item ] ) );
Expand Down
18 changes: 18 additions & 0 deletions tests/php/TemplatingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ public function testTemplateWithForLoopAndSingleElementInArrayToIterate_Rendered
$this->assertSame( '<p><a></a></p>', $result );
}

public function testTemplateWithForLoopAndMemberExpressionForData_RenderedOnce() {
$result = $this->createAndRender(
'<p><a v-for="item in list.data"></a></p>',
[ 'list' => [ 'data' => [ 1 ] ] ]
);

$this->assertSame( '<p><a></a></p>', $result );
}

public function testTemplateWithForLoopAndMultipleElementsInArrayToIterate_RenderedMultipleTimes() {
$result = $this->createAndRender(
'<p><a v-for="item in list"></a></p>',
Expand All @@ -225,6 +234,15 @@ public function testTemplateWithForLoopMustache_RendersCorrectValues() {
$this->assertSame( '<p><a>1</a><a>2</a></p>', $result );
}

public function testTemplateWithForLoopAndKey_DropsKeyAttributeFromOutput() {
$result = $this->createAndRender(
'<p><a v-for="item in list" :key="item">{{item}}</a></p>',
[ 'list' => [ 1, 2 ] ]
);

$this->assertSame( '<p><a>1</a><a>2</a></p>', $result );
}

public function testTemplateWithAttributeBinding_ConditionIsFalse_AttributeIsNotRendered() {
$result = $this->createAndRender( '<p :attr1="condition"></p>', [ 'condition' => false ] );

Expand Down