Skip to content

Commit b6898f3

Browse files
authored
Merge pull request #35 from Paneon/master
Update master
2 parents d7e38c2 + 4bb2141 commit b6898f3

File tree

6 files changed

+72
-6
lines changed

6 files changed

+72
-6
lines changed

src/Compiler.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,21 @@ class Compiler
111111
*/
112112
protected $includeAttributes = ['class', 'style'];
113113

114+
/**
115+
* @var string|null
116+
*/
117+
protected $vBind = null;
118+
114119
/**
115120
* @var string[]
116121
*/
117122
protected $attributesWithIf = ['checked', 'selected', 'disabled'];
118123

124+
/**
125+
* @var array
126+
*/
127+
protected $slotFallbackCounter = [];
128+
119129
/**
120130
* Compiler constructor.
121131
*/
@@ -340,7 +350,8 @@ public function convertNode(DOMNode $node, int $level = 0): DOMNode
340350
$include = $this->document->createTextNode(
341351
$this->builder->createIncludePartial(
342352
$usedComponent->getPath(),
343-
$this->preparePropertiesForInclude($usedComponent->getProperties())
353+
$this->preparePropertiesForInclude($usedComponent->getProperties()),
354+
$this->vBind
344355
)
345356
);
346357

@@ -435,6 +446,9 @@ private function preparePropertiesForInclude(array $variables): array
435446
'dataScopedStyleAttribute|default(\'\')',
436447
false
437448
);
449+
} elseif ($name === 'vBind') {
450+
$this->vBind = $value;
451+
unset($variables[$key]);
438452
}
439453
}
440454

@@ -1297,10 +1311,18 @@ protected function handleSlots(DOMElement $node): void
12971311

12981312
$slotName = Slot::SLOT_PREFIX;
12991313
$slotName .= $node->getAttribute('name') ? $node->getAttribute('name') : Slot::SLOT_DEFAULT_NAME;
1314+
$slotFallbackKey = $slotName . '_fallback';
13001315

13011316
if ($slotFallback) {
1302-
$this->addVariable($slotName . '_fallback', $slotFallback);
1303-
$variable = $this->builder->createVariableOutput($slotName, $slotName . '_fallback');
1317+
if (isset($this->slotFallbackCounter[$slotFallbackKey])) {
1318+
++$this->slotFallbackCounter[$slotFallbackKey];
1319+
$slotFallbackName = $slotFallbackKey . '_' . $this->slotFallbackCounter[$slotFallbackKey];
1320+
} else {
1321+
$this->slotFallbackCounter[$slotFallbackKey] = 1;
1322+
$slotFallbackName = $slotFallbackKey;
1323+
}
1324+
$this->addVariable($slotFallbackName, $slotFallback);
1325+
$variable = $this->builder->createVariableOutput($slotName, $slotFallbackName);
13041326
} else {
13051327
$variable = $this->builder->createVariableOutput($slotName);
13061328
}
@@ -1321,7 +1343,7 @@ protected function handleNamedSlotsInclude(DOMNode $node, Component $usedCompone
13211343
foreach ($node->childNodes as $childNode) {
13221344
if ($childNode instanceof DOMElement && $childNode->tagName === 'template') {
13231345
foreach ($childNode->attributes as $attribute) {
1324-
if ($attribute instanceof DOMAttr && preg_match('/v-slot(?::([a-z]+)?)/i', $attribute->nodeName, $matches)) {
1346+
if ($attribute instanceof DOMAttr && preg_match('/v-slot(?::([a-z0-9_-]+)?)/i', $attribute->nodeName, $matches)) {
13251347
$slotName = $matches[1] ?? Slot::SLOT_DEFAULT_NAME;
13261348
$this->addSlot($slotName, $childNode, $usedComponent);
13271349
$removeNodes[] = $childNode;

src/Utils/TwigBuilder.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,17 @@ public function createBlock(string $content): string
153153
/**
154154
* @param Property[] $variables
155155
*/
156-
public function createIncludePartial(string $partialPath, array $variables = []): string
156+
public function createIncludePartial(string $partialPath, array $variables = [], ?string $vBind = null): string
157157
{
158158
$serializedProperties = $this->serializeComponentProperties($variables);
159159

160-
return $this->createBlock('include "' . $partialPath . '" with ' . $serializedProperties);
160+
$content = 'include "' . $partialPath . '" with ' . $serializedProperties;
161+
162+
if ($vBind) {
163+
$content .= '|merge(' . trim($vBind, '"') . ')';
164+
}
165+
166+
return $this->createBlock($content);
161167
}
162168

163169
/**
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="{{ class|default('') }}" style="{{ style|default('') }}">
2+
{% include "/templates/ChildComponent.twig" with { 'bar': "baz", 'slot_default': "", 'class': "", 'style': "" }|merge(foo) %}
3+
</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<template>
2+
<div>
3+
<ChildComponent v-bind="foo" bar="baz" />
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
name: 'ComponentWithVBind',
10+
};
11+
</script>
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)