Skip to content

Commit 36c63f4

Browse files
authored
Merge pull request #85 from tronsha/bugfix/class-for-component-on-root
Fix include component on root
2 parents dd13585 + 5548030 commit 36c63f4

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

src/Compiler.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ public function convertNode(DOMNode $node, int $level = 0): DOMNode
350350
$include = $this->document->createTextNode(
351351
$this->builder->createIncludePartial(
352352
$usedComponent->getPath(),
353-
$this->preparePropertiesForInclude($usedComponent->getProperties()),
353+
$this->preparePropertiesForInclude($usedComponent->getProperties(), $level === 1),
354354
$this->vBind
355355
)
356356
);
@@ -414,7 +414,7 @@ public function convertNode(DOMNode $node, int $level = 0): DOMNode
414414
*
415415
* @return Property[]
416416
*/
417-
private function preparePropertiesForInclude(array $variables): array
417+
private function preparePropertiesForInclude(array $variables, bool $isRootNode = false): array
418418
{
419419
$values = [];
420420
$hasScopedStyleAttribute = false;
@@ -457,11 +457,15 @@ private function preparePropertiesForInclude(array $variables): array
457457
if ($attribute === 'style') {
458458
$glue = ' ~ "; " ~ ';
459459
}
460-
$variables[] = new Property(
461-
$attribute,
462-
$values[$attribute] ?? null ? implode($glue, $values[$attribute]) : '""',
463-
false
464-
);
460+
$value = $values[$attribute] ?? null ? implode($glue, $values[$attribute]) : '""';
461+
if ($isRootNode) {
462+
$value = $value . $glue . $attribute . '|default(\'\')';
463+
}
464+
$variables[] = new Property($attribute, $value, false);
465+
}
466+
467+
if ($isRootNode) {
468+
$variables[] = new Property('dataScopedStyleAttribute', 'dataScopedStyleAttribute|default(\'\')', false);
465469
}
466470

467471
return $variables;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% include "/templates/ChildComponent.twig" with { 'slot_default': "", 'class': "foo" ~ " " ~ class|default(''), 'style': "color: black" ~ "; " ~ style|default(''), 'dataScopedStyleAttribute': dataScopedStyleAttribute|default('') } %}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<template>
2+
<ChildComponent class="foo" style="color: black" />
3+
</template>
4+
5+
<script>
6+
export default {
7+
name: 'ComponentWithComponentOnRoot',
8+
component: {
9+
ChildComponent,
10+
}
11+
};
12+
</script>

0 commit comments

Comments
 (0)