Skip to content

Commit 8d69b22

Browse files
committed
Add support for v-else-if attributes
php-vuejs-templating already supports `v-if` and `v-else` attributes. Add support for `v-else-if` to complete the conditional node support. Bug: T398318
1 parent e214234 commit 8d69b22

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Component.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ private function handleIf( DOMNodeList $nodes, array $data ) {
188188
// Iteration of iterator breaks if we try to remove items while iterating, so defer node
189189
// removing until finished iterating.
190190
$nodesToRemove = [];
191+
$previousIfCondition = false;
191192
foreach ( $nodes as $node ) {
192193
if ( $this->isTextNode( $node ) ) {
193194
continue;
@@ -203,7 +204,17 @@ private function handleIf( DOMNodeList $nodes, array $data ) {
203204
$nodesToRemove[] = $node;
204205
}
205206

206-
$previousIfCondition = $condition;
207+
$previousIfCondition = $previousIfCondition || $condition;
208+
} elseif ( $node->hasAttribute( 'v-else-if' ) ) {
209+
$conditionString = $node->getAttribute( 'v-else-if' );
210+
$node->removeAttribute( 'v-else-if' );
211+
$condition = $this->app->evaluateExpression( $conditionString, $data );
212+
213+
if ( !$condition ) {
214+
$nodesToRemove[] = $node;
215+
}
216+
217+
$previousIfCondition = $previousIfCondition || $condition;
207218
} elseif ( $node->hasAttribute( 'v-else' ) ) {
208219
$node->removeAttribute( 'v-else' );
209220

tests/php/TemplatingTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ public function testTemplateWithIfElseBlockAndNontruthfulCondition_ElseIsDisplay
192192
$this->assertSame( '<p><a>else</a></p>', $result );
193193
}
194194

195+
public function testTemplateWithElseIfBlockAndTruthfulCondition_IfAndElseRemoved() {
196+
$result = $this->createAndRender(
197+
'<p><a v-if="variable">if</a><a v-else-if="other_variable">else if</a>' .
198+
'<a v-else>else</a></p>',
199+
[ 'variable' => false, 'other_variable' => true ]
200+
);
201+
202+
$this->assertSame( '<p><a>else if</a></p>', $result );
203+
}
204+
195205
public function testTemplateWithForLoopAndEmptyArrayToIterate_NotRendered() {
196206
$result = $this->createAndRender( '<p><a v-for="item in list"></a></p>', [ 'list' => [] ] );
197207

0 commit comments

Comments
 (0)