Skip to content

Commit b1bdc0b

Browse files
authored
Merge pull request #10 from Paneon/develop
Develop
2 parents d4b6489 + 4ca3876 commit b1bdc0b

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ Compile vue files to twig templates with PHP
1616
|v-else-if|:white_check_mark:|
1717
|v-for|:white_check_mark:|
1818
|v-on|:white_check_mark:|
19-
|v-bind||
20-
|v-bind:style||
21-
|v-bind:class||
19+
|v-bind|partially working|
20+
|v-bind:style|partially working|
21+
|v-bind:class|partially working|
2222
|v-model||
2323
|v-pre||
2424
|v-cloak||
@@ -30,7 +30,7 @@ Compile vue files to twig templates with PHP
3030
|Functionality|Implemented|
3131
|:------------|:---------:|
3232
|Slots||
33-
|Components||
33+
|Components|:white_check_mark:|
3434
|Filters||
3535

3636

src/Compiler.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,32 +105,26 @@ public function convertNode(DOMNode $node): DOMNode
105105
$currentComponent = $this->components[$node->nodeName];
106106
$this->handleIf($node);
107107
$this->handleFor($node);
108+
108109
if ($node->hasAttributes()) {
109110
/** @var DOMAttr $attribute */
110111
foreach ($node->attributes as $attribute) {
111112
if (strpos($attribute->name, 'v-bind:') === 0 || strpos($attribute->name, ':') === 0) {
112-
$currentComponent->addProperty($attribute->name, $attribute->value, true);
113-
} else {
114-
$currentComponent->addProperty($attribute->name, $attribute->value, false);
115-
}
116-
}
117-
}
118-
$props = [];
119-
if (count($currentComponent->getProperties()) > 0) {
120-
foreach ($currentComponent->getProperties() as $property) {
121-
if ($property->isBinding()){
122-
$propName = substr($property->getName(), 1); //delete ':'
123-
$props[] = $propName.': '.$property->getValue();
113+
$name = substr($attribute->name, strpos($attribute->name, ':') + 1);
114+
$currentComponent->addProperty($name, $attribute->value, true);
124115
} else {
125-
$props[] = $property->getName().': "'.$property->getValue().'"';
116+
$currentComponent->addProperty($attribute->name, '"'.$attribute->value.'"', false);
126117
}
127118
}
128119
}
129-
$propsString = '';
130-
if (count($props) > 0) {
131-
$propsString = 'with { '.implode(', ', $props).' } ';
132-
}
133-
$include = $this->document->createTextNode('{% include "'.$currentComponent->getPath().'" '.$propsString.'%}');
120+
121+
$include = $this->document->createTextNode(
122+
$this->builder->createIncludePartial(
123+
$currentComponent->getPath(),
124+
$currentComponent->getProperties()
125+
)
126+
);
127+
134128
$node->parentNode->insertBefore($include, $node);
135129
$node->parentNode->removeChild($node);
136130
return $node;

src/Utils/TwigBuilder.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Paneon\VueToTwig\Utils;
44

5+
use Paneon\VueToTwig\Property;
6+
57
class TwigBuilder
68
{
79
protected const OPEN = 0;
@@ -57,19 +59,17 @@ public function createForKeyInList(string $key, string $list)
5759

5860
public function createFor(string $list, ?string $item = null, ?string $key = null)
5961
{
60-
if($item !== null && $key !== null) {
61-
return $this->createBlock( 'for '.$key.', '.$item.' in '.$list);
62-
}
63-
elseif($item !== null) {
62+
if ($item !== null && $key !== null) {
63+
return $this->createBlock('for '.$key.', '.$item.' in '.$list);
64+
} elseif ($item !== null) {
6465
return $this->createForItemInList($item, $list);
65-
}
66-
elseif($key !== null) {
66+
} elseif ($key !== null) {
6767
return $this->createForKeyInList($key, $list);
6868
}
6969

7070
return null;
7171
}
72-
72+
7373
public function createEndFor()
7474
{
7575
return $this->createBlock('endfor');
@@ -89,4 +89,24 @@ public function createBlock($content)
8989
{
9090
return $this->options['tag_block'][self::OPEN].' '.$content.' '.$this->options['tag_block'][self::CLOSE];
9191
}
92+
93+
/**
94+
* @param string $partialPath
95+
* @param Property[] $variables
96+
*/
97+
public function createIncludePartial(string $partialPath, array $variables = [])
98+
{
99+
if (empty($variables)) {
100+
return $this->createBlock('include "'.$partialPath.'"');
101+
}
102+
103+
$props = [];
104+
105+
/** @var Property $property */
106+
foreach ($variables as $property) {
107+
$props[] = $property->getName().': '.$property->getValue();
108+
}
109+
110+
return $this->createBlock('include "'.$partialPath.'" with { '.implode(', ', $props).' }');
111+
}
92112
}

0 commit comments

Comments
 (0)