Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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:

<script type="text/javascript">{ document.write(_ibp_phone_number); }</script>
share|improve this question

2 Answers 2

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 at {{ phone.number }}!
share|improve this answer
    
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 Jul 19 at 6:48
    
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 Jul 19 at 6:56
    
Doesn't seem to work -- but wouldn't this also make the WYSIWYG's <p> <h1> tags show up? –  Jason D Jul 19 at 7:29
    
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 Jul 19 at 16:47
    
Thanks @BenParizek. –  Tim Kelty Aug 27 at 13:53

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}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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