Could you help me with something?

In a frontend template, I'm trying to go through the string, find all substrings between double curly brackets {{ something }} using regular expressions and replace them with field values that have the substring in the name. I want it to work dynamically.

i.e. replace string {{ something }} with value of field {{ entry.something }}, replace string {{ something-else }} with value of field {{ entry.something-else }} ...etc.

I tried the replace filter, and I'm able to find the strings between curly brackets, but I don't know how to replace them with actual fields values.


1 Answer 1


It sounds like you'd be better off using Yii's Message Translations for this, if that's possible in your situation. They are what powers Craft's |t / |translate filter:

{{ "String with {something} and {somethingElse}"|t('site', entry) }}

With this, {something} and {somethingElse} would be replaced with the string values of entry.something and entry.somethingElse.

You can also pass an array for more complex substitutions:

{{ "String with {something} and {somethingElse}"|t('site', {
    something: entry.something,
    somethingElse: entry.somethingElse,
}) }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.