6

I'm trying to use the Encode Email plugin to encode email addresses.

The email addresses are dynamic so I need to include the output from the database when I'm using set.

This is their example that doesn't use dynamic info:

{% set email = "<a href='mailto:you@example.com'>Your Name</a>" %}
{{ email | rot13 }}

And this is what I've tried that doesn't work.

{% set email = "<a href='mailto:{{ contactInfo.contactEmail }}'>{{ contactInfo.contactEmail }}</a>" %}
{{ email | rot13 }}

Is there a way to include the dynamic variables into the set statement?

4

The main thing to remember about Twig... You never need to nest tags within other tags. So putting {{ }} within {% %} is never going to work.

To concatenate a string in Twig, use the ~ character...

{% set email = "<a href='mailto:" ~ contactInfo.contactEmail ~ "'>" ~ contactInfo.contactEmail ~ "</a>" %}
  • Thanks so much! Never knew about concatenation in Twig so that's great to learn you can do that. Thanks again. – markstewie Sep 26 '14 at 2:52
  • You're very welcome! – Lindsey D Sep 26 '14 at 2:54
4

Another way to do this is to use the other set syntax:

{% set email %}
  <a href="mailto:{{ contactInfo.contactEmail }}">{{ contactInfo.contactEmail }}</a>
{% endset %}

Sometimes it is easier to read.

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.