17

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>
24

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.

| 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 '14 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 '14 at 6:56
  • 1
    Doesn't seem to work -- but wouldn't this also make the WYSIWYG's <p> <h1> tags show up? – Jason D Jul 19 '14 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 '14 at 16:47
  • 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 Aug 27 '14 at 14:01
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}

| improve this answer | |
  • 2
    Reference tags only work on plain text fields, not rich text fields. – Dan Herd Sep 18 '15 at 9:40
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!

| improve this answer | |
2

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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