I have a redactor field whose HTML contents contain links to other parts of the website.

How do I render variables located in the HTML content of a Redactor field in Twig?

Sample redactor content:

  <li><a href="{{ siteUrl }}about">About</a></li>
  <li><a href="{{ siteUrl }}contact">Contact</a></li>

In output of this results in the braces being encoded:

  <li><a href="%7B%7B%20siteUrl%20%7D%7D/about">About</a></li>
  <li><a href="%7B%7B%20siteUrl%20%7D%7D/contact">Contact</a></li>

But I want it the output to be whatever siteUrl is. Eg on localhost:

  <li><a href="http://localhost/about">About</a></li>
  <li><a href="http://localhost/contact">Contact</a></li>

Redactor fields are not meant to process twig both for security reasons (as they can be changed by any user) and for usability (you cannot expect authors to remember / preserve twig tags).

In your specific example, the site url is probably not necessary (starting a URL with / will link to the current domain just fine) - so you would not actually need twig.

However I cannot think of one good reason to build a menu in a redactor field. I would either include it directly in the page templates (.twig files) or use a matrix field as a content builder.

| improve this answer | |
  • Yeah, it isn't clean, but it's actually part of data we are migrating over from client's older project. – Davina Leong Apr 2 '19 at 14:04

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.