<p>I know three ways:</p>
<ul>
<li><p><strong>Using <code>format()</code> (look for <a href="https://www.php.net/manual/en/function.sprintf.php" rel="nofollow noreferrer">type specifiers</a>)</strong></p>
<pre><code> {% set string = &quot;There is an %s.&quot;|format(&quot;apple&quot;) %}
</code></pre>
</li>
<li><p><strong>Using string interpolation (assuming that <code>apple=&quot;apple&quot;</code>)</strong></p>
<pre><code> {% set string = &quot;There is an #{apple}.&quot; %}
</code></pre>
</li>
<li><p><strong>Using string concatenation (assuming that <code>apple=&quot;apple&quot;</code>)</strong></p>
<pre><code> {% set string = &quot;There is an &quot; ~ apple %}
</code></pre>
</li>
</ul>

CC BY-SA 3.0
2014-06-18T02:05:29.890Z
i recommend string interpolation become the informal standard IMHO.
Luke Holder
CC BY-SA 3.0
2014-06-14T21:39:40.937Z
2014-06-14T21:39:40.937Z
2023-11-17T20:15:17.737Z
nicael
<p>If you‘ve got a multi-lingual site (or even if you don’t), you can pass variables into strings with the <a href="http://buildwithcraft.com/docs/templating/filters#translate-or-t"><code>|t</code> filter</a>:</p>

<pre><code>{{ "Welcome back, {name}"|t( {'name': currentUser.name} ) }}
</code></pre>

<p>(See <a href="http://buildwithcraft.com/help/static-translations">http://buildwithcraft.com/help/static-translations</a> for details on how you can use the <code>|t</code> filter to translate your text.)</p>

CC BY-SA 3.0
2014-06-14T22:04:24.530Z
2014-06-14T22:04:24.530Z
Brandon Kelly
<p>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?</p>

<p>For example, I have a variable called <code>apple</code> and its value is "Arlington Pippin" (it's a <a href="http://www.orangepippin.com/apples/arlington-pippin" rel="noreferrer">type of apple</a>). I'd like to end up with a phrase that reads:</p>

<blockquote>
  <p>There is an Arlington Pippin.</p>
</blockquote>

<p>How could I accomplish this?</p>

CC BY-SA 3.0
2014-06-18T19:56:35.930Z
Yep, I like apples.
nicael
<templating><string><variable><concatenation>
How can I incorporate a variable into a string?
# How can I incorporate a variable into a string?

(Asked by: username_0 on 2014-06-14)

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](http://www.orangepippin.com/apples/arlington-pippin)). I'd like to end up with a phrase that reads:

> There is an Arlington Pippin.

How could I accomplish this?

**Tags:** templating, string, variable, concatenation
> **Comments:**
> **username_0**: Yep, I like apples.  

---

## 2 answers

### Answer by username_2 on 2014-06-14. Score: 18 | Accepted answer

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.)


---

### Answer by username_0 on 2014-06-14. Score: 18

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 %}

> **Comments:**
> **username_1**: i recommend string interpolation become the informal standard IMHO.  

---

