Skip to content

Commit 417adda

Browse files
committed
Fix slot fallback
1 parent d7e38c2 commit 417adda

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/Compiler.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ class Compiler
116116
*/
117117
protected $attributesWithIf = ['checked', 'selected', 'disabled'];
118118

119+
/**
120+
* @var array
121+
*/
122+
protected $slotFallbackCounter = [];
123+
119124
/**
120125
* Compiler constructor.
121126
*/
@@ -1299,8 +1304,15 @@ protected function handleSlots(DOMElement $node): void
12991304
$slotName .= $node->getAttribute('name') ? $node->getAttribute('name') : Slot::SLOT_DEFAULT_NAME;
13001305

13011306
if ($slotFallback) {
1302-
$this->addVariable($slotName . '_fallback', $slotFallback);
1303-
$variable = $this->builder->createVariableOutput($slotName, $slotName . '_fallback');
1307+
if (isset($this->slotFallbackCounter[$slotName . '_fallback'])) {
1308+
++$this->slotFallbackCounter[$slotName . '_fallback'];
1309+
$slotFallbackName = $slotName . '_fallback_' . $this->slotFallbackCounter[$slotName . '_fallback'];
1310+
} else {
1311+
$this->slotFallbackCounter[$slotName . '_fallback'] = 1;
1312+
$slotFallbackName = $slotName . '_fallback';
1313+
}
1314+
$this->addVariable($slotFallbackName, $slotFallback);
1315+
$variable = $this->builder->createVariableOutput($slotName, $slotFallbackName);
13041316
} else {
13051317
$variable = $this->builder->createVariableOutput($slotName);
13061318
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% set slot_default_fallback %}{{ text }}{% endset %}
2+
{% set slot_default_fallback_2 %}{{ text }}{% endset %}
3+
{% if true %}
4+
<a href="#" class="{{ class|default('') }}" style="{{ style|default('') }}">
5+
{{ slot_default|default(slot_default_fallback) }}
6+
</a>
7+
{% else %}
8+
<button class="{{ class|default('') }}" style="{{ style|default('') }}">
9+
{{ slot_default|default(slot_default_fallback_2) }}
10+
</button>
11+
{% endif %}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<a v-if="true" href="#">
3+
<slot>{{ text }}</slot>
4+
</a>
5+
<button v-else>
6+
<slot>{{ text }}</slot>
7+
</button>
8+
</template>
9+
<script>
10+
export default {
11+
name: 'ComponentWithTowDefaultSlots',
12+
};
13+
</script>

0 commit comments

Comments
 (0)