Skip to content

Commit c828215

Browse files
authored
Merge pull request #11 from Paneon/feature/SHODVES2-122
SHODEVS2-122 extend compiler with template strings in components
2 parents b1bdc0b + cf59fad commit c828215

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

src/Utils/TwigBuilder.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ public function createBlock($content)
9292

9393
/**
9494
* @param string $partialPath
95-
* @param Property[] $variables
95+
* @param Property[] $variables
96+
*
97+
* @return string
9698
*/
9799
public function createIncludePartial(string $partialPath, array $variables = [])
98100
{
@@ -104,9 +106,26 @@ public function createIncludePartial(string $partialPath, array $variables = [])
104106

105107
/** @var Property $property */
106108
foreach ($variables as $property) {
107-
$props[] = $property->getName().': '.$property->getValue();
109+
if($property->getName() !== 'key') {
110+
$value = $this->checkPropertyValue($property->getValue());
111+
$props[] = $property->getName().': '.$value;
112+
}
108113
}
109114

110115
return $this->createBlock('include "'.$partialPath.'" with { '.implode(', ', $props).' }');
111116
}
117+
118+
public function checkPropertyValue($value)
119+
{
120+
if (preg_match('/^`(?P<content>.+)`$/', $value, $matches)) {
121+
$templateStringContent = '"'.$matches['content'].'"';
122+
$value = preg_replace(
123+
'/\$\{(.+)\}/',
124+
'{{ $1 }}',
125+
$templateStringContent
126+
);
127+
}
128+
129+
return $value;
130+
}
112131
}

tests/VueComponentBindingTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Paneon\VueToTwig\Tests;
4+
5+
class VueComponentBindingTest extends AbstractTestCase
6+
{
7+
/**
8+
* @dataProvider dataProvider
9+
* @throws \Exception
10+
*/
11+
public function testComponentBinding($html, $expected)
12+
{
13+
$compiler = $this->createCompiler($html);
14+
15+
$compiler->registerComponent('ChildComponent', '/templates/ChildComponent.twig');
16+
17+
$actual = $compiler->convert();
18+
19+
$this->assertEqualHtml($expected, $actual);
20+
}
21+
22+
public function dataProvider()
23+
{
24+
return $this->loadFixturesFromDir('vue-component');
25+
}
26+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
{% include "/templates/ChildComponent.twig" with { style: "fill:{{ color }};", test: "test" } %}
3+
</div>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<div>
3+
<ChildComponent :style="`fill:${color};`" :key=testkey test="test" />
4+
</div>
5+
</template>
6+
<script>
7+
export default {
8+
name: 'component-with-child-binding.vue',
9+
component: {
10+
ChildComponent,
11+
}
12+
};
13+
</script>

0 commit comments

Comments
 (0)