Skip to content

Commit 595162f

Browse files
authored
Merge pull request #77 from tronsha/bugfix/binding-with-template-string-4
Added more support for binding with template string
2 parents 1773bf0 + 48de0d6 commit 595162f

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/Compiler.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,12 @@ public function handleBinding(string $value, string $name, ?DOMElement $node = n
675675
} else {
676676
$value = $this->builder->refactorCondition($value);
677677
$this->logger->debug(sprintf('- setAttribute "%s" with value "%s"', $name, $value));
678+
if (substr_count($value, '`')) {
679+
preg_match_all('/`[^`]+`/', $value, $matches);
680+
foreach ($matches as $match) {
681+
$value = str_replace($match[0], $this->refactorTemplateString($match[0]), $value);
682+
}
683+
}
678684
$dynamicValues[] = $this->builder->prepareBindingOutput($value, $twigOutput);
679685
}
680686

@@ -1080,7 +1086,7 @@ public function refactorTemplateString(string $value): string
10801086
if (preg_match('/^`(?P<content>.+)`$/', $value, $matches)) {
10811087
$templateStringContent = '"' . $matches['content'] . '"';
10821088
$value = preg_replace(
1083-
'/\${(.+)}/',
1089+
'/\${([^{}]+)}/',
10841090
'" ~ ( $1 ) ~ "',
10851091
$templateStringContent
10861092
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% set foo = foo|default('') %}
2+
<div class="block {{ class|default('') }}" style="{{ style|default('') }}">
3+
<div style="width: {{ size }}px; height: {{ size }}px;">
4+
Foo
5+
</div>
6+
<div style='{{ foo ? foo : "width: " ~ ( size ) ~ "px; height: " ~ ( size ) ~ "px;" }};'>
7+
Bar
8+
</div>
9+
</div>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<template>
2+
<div class="block">
3+
<div :style="`width: ${size}px; height: ${size}px;`">
4+
Foo
5+
</div>
6+
<div :style="foo ? foo : `width: ${size}px; height: ${size}px;`">
7+
Bar
8+
</div>
9+
</div>
10+
</template>
11+
12+
<script>
13+
export default {
14+
props: {
15+
foo: {
16+
type: String,
17+
default: '',
18+
},
19+
size: {
20+
type: Number,
21+
required: true,
22+
},
23+
},
24+
};
25+
</script>

0 commit comments

Comments
 (0)