CMS Development

blowltd
Membre

if statement on custom properties in Email

Résolue

Hi,

I am trying to send a transactional email with Hubspot.

This email is HTML+Hubl and for some reason my If statement is not working as expected.

 

So we are sending a custom.properties sometimes with content to render and sometimes not.

so first I thought: Let's try to check if this property "is defined",

unfortunately, the API make mandatory to receive this custom property so "is defined" does not work.

 

Then I thought let's always send something but when I have no content, set this property to "null" or false or a string.

Then I tried to test if my property is one of those and it is not working...

 

I am lost, does someone has any idea?

 

1 Solution acceptée
Solution
Anonymous
Non applicable

if statement on custom properties in Email

Résolue

Hi @blowltd,

I've used HubL properties several times in my emails. 

Everytime there is a property and I need to do conditional, I just use,

{% if variable_name %}show your content{% endif %}


I use it the above way when I am using Custom Modules for my Email.

If I want to use normal HubL variables, 

For Rich Text,

{% rich_text "my_module" label='My rich text module', html='Default module text' %}
{% if "my_module" %}
A module named "my_module" is defined in this template.
{% endif %}

For text fields,

{% text "product_names" label='Enter the product names that you would like to render the coupon ad for', value='all of our products', export_to_template_context=True %}

{% if widget_data.product_names.value %}
<div class="coupon-ad">
<h3>For a limited time, get 50% off {{ widget_data.product_names.value}}! </h3>
</div>
{% endif %}

For choice fields,

{% choice "department" label='Choose department', value='Marketing', choices='Marketing, Sales, Dev, Services' export_to_template_context=True %}
        
{% if widget_data.department.value == 'Marketing' %}

<h3>Want to join our amazing Marketing team?!</h3>
<h4>We have exciting career opportunities on the {{ widget_data.department.value }} team.</h4>

{% elif widget_data.department.value == 'Sales' %}

<h3>Are you a Sales superstar?</h3>
<h4>We have exciting career opportunities on the {{ widget_data.department.value }} team.</h4>        
        
{% elif widget_data.department.value == 'Dev' %}
        
<h3>Do you love to ship code?</h3>
<h4>We have exciting career opportunities on the {{ widget_data.department.value }} team.</h4>
        
{% else %}
        
<h3>Want to work with our awesome customers?</h3>
<h4>We have exciting career opportunities on the {{ widget_data.department.value }} team.</h4>
        
{% endif %}   

For Checkbox / Boolean,

{% boolean "check_box" label='Show alert', value=True, export_to_template_context=True %}

{% if widget_data.check_box.value is truthy %}
<div class='alert'>Danger!</div>
{% endif %}


Let me know if it helps...

Voir la solution dans l'envoi d'origine

6 Réponses
kincorvia
Participant

if statement on custom properties in Email

Résolue

I have the same issue, did you ever resolve this? The accepted answer does not seem relevant to the issues we are facing with transactional emails using the single send API.

BiG
Contributeur

if statement on custom properties in Email

Résolue

I agree.

This is not a real solution to me.

 

I have the same issue.
See also:

https://community.hubspot.com/t5/CMS-Development/if-statements-in-email-templates/m-p/282907

Please support this idea which queries for if statements in HUBL for emails:
https://community.hubspot.com/t5/HubSpot-Ideas/if-statements-in-email-templates/idi-p/282907

0 Votes
Solution
Anonymous
Non applicable

if statement on custom properties in Email

Résolue

Hi @blowltd,

I've used HubL properties several times in my emails. 

Everytime there is a property and I need to do conditional, I just use,

{% if variable_name %}show your content{% endif %}


I use it the above way when I am using Custom Modules for my Email.

If I want to use normal HubL variables, 

For Rich Text,

{% rich_text "my_module" label='My rich text module', html='Default module text' %}
{% if "my_module" %}
A module named "my_module" is defined in this template.
{% endif %}

For text fields,

{% text "product_names" label='Enter the product names that you would like to render the coupon ad for', value='all of our products', export_to_template_context=True %}

{% if widget_data.product_names.value %}
<div class="coupon-ad">
<h3>For a limited time, get 50% off {{ widget_data.product_names.value}}! </h3>
</div>
{% endif %}

For choice fields,

{% choice "department" label='Choose department', value='Marketing', choices='Marketing, Sales, Dev, Services' export_to_template_context=True %}
        
{% if widget_data.department.value == 'Marketing' %}

<h3>Want to join our amazing Marketing team?!</h3>
<h4>We have exciting career opportunities on the {{ widget_data.department.value }} team.</h4>

{% elif widget_data.department.value == 'Sales' %}

<h3>Are you a Sales superstar?</h3>
<h4>We have exciting career opportunities on the {{ widget_data.department.value }} team.</h4>        
        
{% elif widget_data.department.value == 'Dev' %}
        
<h3>Do you love to ship code?</h3>
<h4>We have exciting career opportunities on the {{ widget_data.department.value }} team.</h4>
        
{% else %}
        
<h3>Want to work with our awesome customers?</h3>
<h4>We have exciting career opportunities on the {{ widget_data.department.value }} team.</h4>
        
{% endif %}   

For Checkbox / Boolean,

{% boolean "check_box" label='Show alert', value=True, export_to_template_context=True %}

{% if widget_data.check_box.value is truthy %}
<div class='alert'>Danger!</div>
{% endif %}


Let me know if it helps...

blowltd
Membre

if statement on custom properties in Email

Résolue

Hi,

 

I did try the first one:

{% if custom.my_variable %}
<p> Hello </p>
{% endif %}

but when I send without this "custom.my_variable" to the API

The error I got from the API is:

Mismatch in custom properties between template and request.\n - Custom properties in the template, but not in the request:

So clearly I can't do this test as API refuses not to send the property.

Anonymous
Non applicable

if statement on custom properties in Email

Résolue

@blowltd Can you share your HubL Code declaring the variable?

blowltd
Membre

if statement on custom properties in Email

Résolue

Hi,

 

here is the declaration:

{% if custom.get_ready_3 %}
                                <table align="left" border="0" cellpadding="0" cellspacing="0" style="max-width:300px;" width="100%" class="mcnTextContentContainer">
                                  <tbody>
                                    <tr>
                                      <td valign="top" class="mcnTextContent" style="padding-top:0; padding-left:18px; padding-bottom:9px; padding-right:18px;">
                                        <div style="text-align: center;"><img data-file-id="38533" height="50" src="https://img.blowltd.com/transactional/4.png" style="border: 0px initial ; width: 50px; height: 50px; margin: 0px;" width="50"><br>
                                          <br>
                                          <span style="font-size:15px"><span style="font-family:roboto,helvetica neue,helvetica,arial,sans-serif">{{ custom.get_ready_3 }}</span></span><br>
                                        </div>
                                      </td>
                                    </tr>
                                  </tbody></table>
                                {% endif %} 
0 Votes