<h2>The Technical Way</h2>

<p>You can do this using Twig's <a href="http://twig.sensiolabs.org/doc/functions/template_from_string.html" rel="noreferrer">template_from_string</a> function and the raw filter.  In your template code you would output your Rich Text field:</p>

<pre><code>{{ include(template_from_string(entry.richTextFieldHandle)|raw) }}
</code></pre>

<p>And within your Rich Text field you could reference your global with the same syntax you normally would in Twig as you described above:</p>

<pre><code>This is a rich text field, call us today at {{ phone.number }}!
</code></pre>

<hr>

<h2>A More User-Friendly Way</h2>

<p>The <a href="https://github.com/nystudio107/richvariables" rel="noreferrer">Rich Variables</a> Plugin allows you to give a user control over adding Global Variables to your Rich Text field.</p>

<p>This approach will be a bit more limited in what you can accomplish, but probably toward a more user-friendly outcome for your content managers.</p>

CC BY-SA 3.0
2014-07-19T06:48:57.343Z
Thank you! This works very well. In this particular case I am actually trying to insert HTML as the global (which this doesn't work for)... Do you have any ideas how I could accomplish that? I updated my question to add that detail.
Jason D
CC BY-SA 3.0
2014-07-19T06:56:25.097Z
I believe you'll just need to add the |raw filter to the template_from_string output.  I've updated my answer as well.
Ben Parizek
CC BY-SA 3.0
2014-07-19T07:29:25.820Z
Doesn't seem to work -- but wouldn't this also make the WYSIWYG's <p> <h1> tags show up?
Jason D
CC BY-SA 3.0
2014-07-19T16:47:15.130Z
The raw filter would tells the page it's safe to output the complete variable as html. I don't think it's going to be possible to output one small section of your Rich Text field in a different way than the rest of the field.  You may have to break up your data into two parts: perhaps add the javascript to the template and just output a phone number placeholder in your Rich Text field?
Ben Parizek
CC BY-SA 3.0
2014-08-27T14:01:36.933Z
Ultimately, I think I'd like a more "built-in" solution. It seems crazy to do this with every text/richtext field you output, and it would be confusing for a CP user if you could use a tag in field in not in another.

I think a cool feature request/plugin would be some kind of template "token" system, where you could define available tokens, which could have twig content or whatever, and would get parsed via twig filter. You could then list you available tokens above certain text fields.

Thoughts?
Tim Kelty
CC BY-SA 3.0
2014-07-19T06:34:35.870Z
2017-02-07T00:40:18.830Z
2017-02-07T00:40:18.830Z
Ben Parizek
<p>Craft has a <code>parseRefs</code> filter built right in: <a href="http://buildwithcraft.com/docs/reference-tags">http://buildwithcraft.com/docs/reference-tags</a></p>

<p>One drag if using globals is that you have to use the globalset's ID, not the handle. e.g. <code>{globalset:34:number}</code></p>

CC BY-SA 4.0
2021-12-02T10:16:21.550Z
They do work on rich text fields in 2021. I use them in a rich text block in a matrix field.
nicbou
CC BY-SA 3.0
2014-08-11T20:00:39.457Z
2014-08-11T20:00:39.457Z
Tim Kelty
<p>For those bumping into this <code>old-ish</code> question and looking for a similar solution: Andrew Welch has recently released a plugin that allows you to pull field values from a globalset.</p>

<p><a href="https://github.com/nystudio107/richvariables">https://github.com/nystudio107/richvariables</a></p>

<p>It's not letting you write Twig templates, but it allows you do achieve what is described in the OP, namely output a custom field value inside the Rich Text Editor.</p>

<p>Hope it helps!</p>

CC BY-SA 3.0
2017-02-06T23:02:54.313Z
2017-02-06T23:02:54.313Z
simonswiss
<p>I'm not sure when this changed, but Rich Text fields now appear to be parsed for reference tags (Craft 2.6)</p>

CC BY-SA 3.0
2016-08-08T15:15:11.163Z
2016-08-08T15:15:11.163Z
Tim Kelty
<p>I have global variables that I want to include in Rich Text Fields, for example:</p>

<pre><code>This is a rich text field, call us today at at {{ phone.number }}!
</code></pre>

<p>This doesn't work out-of-the-box, are there any ways of doing this? I am coming from working in another CMS where this was possible to do. </p>

<p>Update: Specifically in this case I the global variable includes a javascript code that dynamically updates the phone number with a tracking number:</p>

<pre><code>&lt;script type="text/javascript"&gt;{ document.write(_ibp_phone_number); }&lt;/script&gt;
</code></pre>

<templating><richtext>
Is it possible to use Twig in a Rich Text Field?
# Is it possible to use Twig in a Rich Text Field?

(Asked by: username_0 on 2014-07-19)

I have global variables that I want to include in Rich Text Fields, for example:
This is a rich text field, call us today at at {{ phone.number }}!

This doesn't work out-of-the-box, are there any ways of doing this? I am coming from working in another CMS where this was possible to do. 
Update: Specifically in this case I the global variable includes a javascript code that dynamically updates the phone number with a tracking number:

**Tags:** templating, richtext


---

## 4 answers

### Answer by username_1 on 2014-07-19. Score: 26

The Technical Way
You can do this using Twig's template_from_string function and the raw filter.  In your template code you would output your Rich Text field:
{{ include(template_from_string(entry.richTextFieldHandle)|raw) }}

And within your Rich Text field you could reference your global with the same syntax you normally would in Twig as you described above:
This is a rich text field, call us today at {{ phone.number }}!


A More User-Friendly Way
The Rich Variables Plugin allows you to give a user control over adding Global Variables to your Rich Text field.
This approach will be a bit more limited in what you can accomplish, but probably toward a more user-friendly outcome for your content managers.

> **Comments:**
> **username_0**: Thank you! This works very well. In this particular case I am actually trying to insert HTML as the global (which this doesn't work for)... Do you have any ideas how I could accomplish that? I updated my question to add that detail.  
> **username_1**: I believe you'll just need to add the |raw filter to the template_from_string output.  I've updated my answer as well.  
> **username_0**: Doesn't seem to work -- but wouldn't this also make the WYSIWYG's <p> <h1> tags show up?  
> **username_1**: The raw filter would tells the page it's safe to output the complete variable as html. I don't think it's going to be possible to output one small section of your Rich Text field in a different way than the rest of the field.  You may have to break up your data into two parts: perhaps add the javascript to the template and just output a phone number placeholder in your Rich Text field?  
> **username_2**: Ultimately, I think I'd like a more "built-in" solution. It seems crazy to do this with every text/richtext field you output, and it would be confusing for a CP user if you could use a tag in field in not in another.  I think a cool feature request/plugin would be some kind of template "token" system, where you could define available tokens, which could have twig content or whatever, and would get parsed via twig filter. You could then list you available tokens above certain text fields.  Thoughts?  

---

### Answer by username_2 on 2014-08-11. Score: 8

Craft has a `parseRefs` filter built right in: <http://buildwithcraft.com/docs/reference-tags>

One drag if using globals is that you have to use the globalset's ID, not the handle. e.g. `{globalset:34:number}`

> **Comments:**
> **username_3**: They do work on rich text fields in 2021. I use them in a rich text block in a matrix field.  

---

### Answer by username_4 on 2017-02-06. Score: 5

For those bumping into this `old-ish` question and looking for a similar solution: Andrew Welch has recently released a plugin that allows you to pull field values from a globalset.

<https://github.com/nystudio107/richvariables>

It's not letting you write Twig templates, but it allows you do achieve what is described in the OP, namely output a custom field value inside the Rich Text Editor.

Hope it helps!


---

### Answer by username_2 on 2016-08-08. Score: 2

I'm not sure when this changed, but Rich Text fields now appear to be parsed for reference tags (Craft 2.6)


---

