Skip to content

Commit

Permalink
Add support for string interpolation to normalizeObjectTemplate()
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Jul 8, 2021
1 parent 74bb735 commit ef28fa6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v3.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
- The `{% cache %}` tag now stores any JavaScript or CSS code registered with `{% js %}`, `{% script %}`, and `{% css %}` tags. ([#7758](https://github.com/craftcms/cms/issues/7758))
- The `date()` Twig function now supports arrays with `date` and/or `time` keys. ([#7681](https://github.com/craftcms/cms/issues/7681))
- Date query params now support passing in times relative to `now` (e.g. `>= now`). ([#9117](https://github.com/craftcms/cms/issues/9117))
- Object templates (such as `redirect` params and URI Format settings) now support string interpolation. ([#9138](https://github.com/craftcms/cms/discussions/9138))
- Custom field column names now include a random string, preventing column name conflicts when deploying multiple project config changes at once. ([#6922](https://github.com/craftcms/cms/issues/6922))
- Custom fields can now store data across multiple columns in the `content` table.
- Channel and Structure sections’ initial entry types are now named “Default” by default. ([#7078](https://github.com/craftcms/cms/issues/7078))
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Changed
- Entries now retain their original IDs when created from an unpublished draft. ([#9104](https://github.com/craftcms/cms/issues/9104))
- Date query params now support passing in times relative to `now` (e.g. `>= now`). ([#9117](https://github.com/craftcms/cms/issues/9117))
- Object templates (such as `redirect` params and URI Format settings) now support string interpolation. ([#9138](https://github.com/craftcms/cms/discussions/9138))
- `craft\helpers\DateTimeHelper::toDateTime()` now supports passing in `'now'`. ([#9117](https://github.com/craftcms/cms/issues/9117))

### Deprecated
Expand Down
15 changes: 9 additions & 6 deletions src/web/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -678,12 +678,15 @@ public function normalizeObjectTemplate(string $template): string
{
$tokens = [];

// Tokenize {% verbatim %} tags
$template = preg_replace_callback('/\{%-?\s*verbatim\s*-?%\}.*?{%-?\s*endverbatim\s*-?%\}/s', function(array $matches) use (&$tokens) {
$token = 'tok_' . StringHelper::randomString(10);
$tokens[$token] = $matches[0];
return $token;
}, $template);
// Tokenize {% verbatim %} and output tags
$template = preg_replace_callback('/\{%-?\s*verbatim\s*-?%\}.*?{%-?\s*endverbatim\s*-?%\}|(?<!\{)\{\{(?!\{).+?(?<!\})\}\}(?!\})/s',
function(array $matches) use (&$tokens) {
$token = 'tok_' . StringHelper::randomString(10);
$tokens[$token] = $matches[0];
return $token;
},
$template
);

// Tokenize inline code and code blocks
$template = preg_replace_callback('/(?<!`)(`|`{3,})(?!`).*?(?<!`)\1(?!`)/s', function(array $matches) use (&$tokens) {
Expand Down
1 change: 1 addition & 0 deletions tests/unit/web/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ public function normalizeObjectTemplateDataProvider(): array
["{% verbatim %}\n{foo}\n{% endverbatim %}", "{% verbatim %}\n{foo}\n{% endverbatim %}"],
["{%- verbatim -%}\n{foo}\n{%- endverbatim -%}", "{%- verbatim -%}\n{foo}\n{%- endverbatim -%}"],
['{{ clone(productCategory).level(1).one().slug|raw }}', '{clone(productCategory).level(1).one().slug}'],
['{{ #{foo} }}', '{{ #{foo} }}'],
];
}

Expand Down

0 comments on commit ef28fa6

Please sign in to comment.