HubL string comparison fails

Contact has a single line text property preferred_branch. Contact record values are “ijmuiden”, “velserbroek”, or empty.
From Design Manager > Email template (HTML and HubL)

{% set branch_key = contact.preferred_branch %}
*{{ branch_key }}*
{{ branch_key }}
{% if branch_key == “ijmuiden” %}
IJMUIDEN
{% elif branch_key == “velserbroek” %}
VELSERBROEK
{% else %}
UNKNOWN
{% endif %}
{{ crm_objects(“contact”, “preferred_branch__eq=ijmuiden”, “preferred_branch”) }}
Email based on this template returns (in Preview mode, for contact with valure “ijmuiden”):
*ijmuiden* ijmuiden UNKNOWN {has_more=false, offset=6, total=6, results=[{preferred_branch=ijmuiden, id=185050575243}, {preferred_branch=ijmuiden, id=185050965806}, {preferred_branch=ijmuiden, id=185058236162}, {preferred_branch=ijmuiden, id=185059223056}, {preferred_branch=ijmuiden, id=185061766250}, {preferred_branch=ijmuiden, id=185061766251}]}

Why is the string comparison failing?

Hi @birdman

Thanks for sharing this — string comparisons in HubL can definitely be tricky even when the value looks correct.

I’ll loop in a few Community members who are very familiar with HubL and email template behavior to weigh in here.
@SteveHTM @alyssamwilie @BarryGrennan — have you seen cases where a HubL string renders correctly but fails an if == comparison? Any known quirks around whitespace, encoding, or email preview context that could explain this?

Appreciate any insight you can share.
Victor

Thanks @Victor_Becerra - I have tried Trim, no success. Lenght is also interesting. It does not return the actual string length but always the same length. If have tried ==, but also ‘IS’.
Hopefully an enligthed spirit will show me the way.

@birdman - I have to say that I am mystified by this one. I have confirmed your observations about the ‘==’ and ‘is string_containing’ tests in an email template, but I think this is not a general string comparison failure, but one that is specific to the manipulation/testing of personalization tokens in the context of an email.

As far as I can tell, all the equality tests perform ‘properly’ in the context of a web page or custom module, and also perform ‘properly’ when the string involved does NOT come from a object reference (a Contact personalization reference at least).

So, is this a bug or an undocumented limitation? I see there is a limitation on the use of HubL filters on personalization tokens in emails - perhaps this is related?

Hope this is helpful.

Steve

Hey @birdman,

Do you have programmable email setting turned on?

I did a quick test with an email module:

{% set firstname = contact.firstname %}

{% if firstname == "John" %}
HEY JOHN
{% elif firstname == "Tom" %}
HEY TOM
{% else %}
HEY STRANGER
{% endif %}

When I preview the email with a contact that matches the first name, I am getting the correct greeting with the example above.

Hello @evaldas - thanks for your answer. This is indeed the solution.
The option ‘Use module for programmable email’ is only available when creating a module
(HTML/CSS/JS), not when creating a template (HTML/HubL).
In my case it seems I have to add in the header ‘-->: isEnabledForEmailV3Rendering: true’, as stated in the KB article you refer to.