How can I incorporate a variable into a string, so that the result is a longer string that includes the contents of a given variable?

For example, I have a variable called apple and its value is "Arlington Pippin" (it's a type of apple). I'd like to end up with a phrase that reads:

There is an Arlington Pippin.

How could I accomplish this?

  • Yep, I like apples.
    – nicael
    Commented Jun 18, 2014 at 19:56

2 Answers 2


If you‘ve got a multi-lingual site (or even if you don’t), you can pass variables into strings with the |t filter:

{{ "Welcome back, {name}"|t( {'name': currentUser.name} ) }}

(See http://buildwithcraft.com/help/static-translations for details on how you can use the |t filter to translate your text.)


I know three ways:

  • Using format() (look for type specifiers)

     {% set string = "There is an %s."|format("apple") %}
  • Using string interpolation (assuming that apple="apple")

     {% set string = "There is an #{apple}." %}
  • Using string concatenation (assuming that apple="apple")

     {% set string = "There is an " ~ apple %}
  • i recommend string interpolation become the informal standard IMHO. Commented Jun 18, 2014 at 2:05

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.