Hi all,
I’m trying to create some dynamic elements on our CMS pages that vary based on the laungage of the page. It seems like I should be able to us content.language to get the language code for the page, but for some reason I can’t seem to get any queries against it to work.
For example, directly printing {{ content.language }} or returns “en”
but if I do:
{% if content.language == "en" %}
<p>Your language is English.</p>
{% else %}
<p>Your language is something else.</p>
{% endif %}
I always get back “Your language is something else”. For some reason the IF statement just isn’t matching the language.
Any suggestions what I’m doing wrong?
Hi SimonH_Demodia,
Thanks for posting this. I’m having an engineer look at this. I think what’s happening is that the {{ content.language }} variable isn’t getting its type set to str, which is what should be happening. You can print the variable because it exists. And if you run a conditional statement like this:
{% if content.language == content.language %}
<p>content.language == content.language</p>
{% endif %}
You should see that it evaluates to true and your <p> tag gets printed. So it is possible to use it in conditional tests. But if you run a type() test on it with something like:
<p>type(content.language): {{ type(content.language) }}</p>
…the type comes back as unknown. Which would explain why it’s not ever equal to “en”, which is a str type. I’ll let you know when I have a better answer for you, but I think this is what’s happening.
Thanks, that looks like exactly what’s happening.
Try this and take a look at the result… {{ content.language|pprint }}
Hi SimonH_Demodia,
Just wanted to update you on this one. We’ve updated our documentation to reflect the fact that content.language is a dict. You can access the string language value from content.language.languageTag . And you can test for the language variation of your page by doing something like:
{% if content.language.languageTag == "en" %}
{# this is the English translate of the page #}
{% endif %}
I hope this helps. Let me know if you have any questions about it.
- Leland