Skip to content

Commit 45bbd10

Browse files
authored
Merge pull request #80 from tronsha/bugfix/slot-fallback
Fix slot fallback
2 parents 5dd1533 + 4f2f6ed commit 45bbd10

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/Compiler.php

Lines changed: 15 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
*/
@@ -1297,10 +1302,18 @@ protected function handleSlots(DOMElement $node): void
12971302

12981303
$slotName = Slot::SLOT_PREFIX;
12991304
$slotName .= $node->getAttribute('name') ? $node->getAttribute('name') : Slot::SLOT_DEFAULT_NAME;
1305+
$slotFallbackKey = $slotName . '_fallback';
13001306

13011307
if ($slotFallback) {
1302-
$this->addVariable($slotName . '_fallback', $slotFallback);
1303-
$variable = $this->builder->createVariableOutput($slotName, $slotName . '_fallback');
1308+
if (isset($this->slotFallbackCounter[$slotFallbackKey])) {
1309+
++$this->slotFallbackCounter[$slotFallbackKey];
1310+
$slotFallbackName = $slotFallbackKey . '_' . $this->slotFallbackCounter[$slotFallbackKey];
1311+
} else {
1312+
$this->slotFallbackCounter[$slotFallbackKey] = 1;
1313+
$slotFallbackName = $slotFallbackKey;
1314+
}
1315+
$this->addVariable($slotFallbackName, $slotFallback);
1316+
$variable = $this->builder->createVariableOutput($slotName, $slotFallbackName);
13041317
} else {
13051318
$variable = $this->builder->createVariableOutput($slotName);
13061319
}
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)