Skip to content

Commit f5d3f59

Browse files
committed
Add escape mechanism for double braces in templates and update related docs/tests
1 parent 0d0ac17 commit f5d3f59

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

docs/api.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ run('echo {{ message | quote }}');
264264
run('grep -r {{ pattern | quote }} {{release_path}}');
265265
```
266266

267+
To output literal `{{` without replacement, escape with a backslash `\{{`:
268+
```php
269+
run('echo \{{not_replaced}}'); // outputs: {{not_replaced}}
270+
```
271+
267272

268273

269274
| Argument | Type | Comment |
@@ -459,7 +464,7 @@ Writes a message to the output and adds a newline at the end.
459464
parse(string $value): string
460465
```
461466

462-
Parse set values.
467+
Parse set values. Use `\{{` to escape brackets and prevent replacement.
463468

464469

465470
## set()

docs/basics.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ task('my_task', function () {
145145
});
146146
```
147147

148+
To output literal `{{` without replacement, escape with a backslash `\{{`:
149+
150+
```php
151+
run('echo \{{not_replaced}}'); // outputs: {{not_replaced}}
152+
```
153+
148154
---
149155

150156
## Global Configurations

src/Configuration.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ public function parse(mixed $value): mixed
115115
{
116116
if (is_string($value)) {
117117
$normalizedValue = normalize_line_endings($value);
118-
return preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*(?:\|\s*(\w+)\s*)?\}\}/', function (array $matches) {
118+
$normalizedValue = str_replace('\{{', "\x00\x00", $normalizedValue);
119+
$result = preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*(?:\|\s*(\w+)\s*)?\}\}/', function (array $matches) {
119120
$value = $this->get($matches[1]);
120121
if (isset($matches[2])) {
121122
$value = match ($matches[2]) {
@@ -125,6 +126,7 @@ public function parse(mixed $value): mixed
125126
}
126127
return $value;
127128
}, $normalizedValue);
129+
return str_replace("\x00\x00", '{{', $result);
128130
}
129131

130132
return $value;

tests/src/Configuration/ConfigurationTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ public function testAddToParentCallback()
159159
self::assertEquals(['a', 'b', 'c'], $alpha->get('files'));
160160
}
161161

162+
public function testParseEscapedBraces()
163+
{
164+
$config = new Configuration();
165+
$config->set('foo', 'a');
166+
167+
self::assertEquals('{{foo}}', $config->parse('\{{foo}}'));
168+
self::assertEquals('a {{bar}}', $config->parse('{{foo}} \{{bar}}'));
169+
self::assertEquals('{{foo}} {{bar}}', $config->parse('\{{foo}} \{{bar}}'));
170+
}
171+
162172
public function testParseQuoteFilter()
163173
{
164174
$config = new Configuration();

0 commit comments

Comments
 (0)