Skip to content

Commit 4bb2141

Browse files
authored
Merge pull request #81 from tronsha/feature/v-bind
Add v-bind support
2 parents 45bbd10 + 6e3bddd commit 4bb2141

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

src/Compiler.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ 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
*/
@@ -345,7 +350,8 @@ public function convertNode(DOMNode $node, int $level = 0): DOMNode
345350
$include = $this->document->createTextNode(
346351
$this->builder->createIncludePartial(
347352
$usedComponent->getPath(),
348-
$this->preparePropertiesForInclude($usedComponent->getProperties())
353+
$this->preparePropertiesForInclude($usedComponent->getProperties()),
354+
$this->vBind
349355
)
350356
);
351357

@@ -440,6 +446,9 @@ private function preparePropertiesForInclude(array $variables): array
440446
'dataScopedStyleAttribute|default(\'\')',
441447
false
442448
);
449+
} elseif ($name === 'vBind') {
450+
$this->vBind = $value;
451+
unset($variables[$key]);
443452
}
444453
}
445454

@@ -1334,7 +1343,7 @@ protected function handleNamedSlotsInclude(DOMNode $node, Component $usedCompone
13341343
foreach ($node->childNodes as $childNode) {
13351344
if ($childNode instanceof DOMElement && $childNode->tagName === 'template') {
13361345
foreach ($childNode->attributes as $attribute) {
1337-
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)) {
13381347
$slotName = $matches[1] ?? Slot::SLOT_DEFAULT_NAME;
13391348
$this->addSlot($slotName, $childNode, $usedComponent);
13401349
$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>

0 commit comments

Comments
 (0)