If rich text field has value then

SOLVE
jau
Regular Contributor

Is there a category for development questions(code questions)? Leaving this uncategorized as there isn't a clear option.

Simple question, Hubl documentation on if statements doesn't seem to cover in regards to what may be an issue with the new design manager.

Custom module. Within that module a Rich text field. Need a simple if statement, if the field has value then do something. Again the documentation doesn't align with the experience of the custom module builder in regards to including fields within it. I've tried a handful of options based on that documentation with no success.

Field name is "heading". How do i simply say IF rich text has value THEN do this thing.

Tried these:

{% if widget_data.heading.value %}
DO THIS THING
{% endif %}

{% if widget_data.heading.src %}
DO THIS THING
{% endif %}

{% if module.heading.value %}
DO THIS THING
{% endif %}

None worked

Reply
0 Upvotes
1 Accepted solution

Accepted Solutions
HubSpot Moderator

Hi @jau, I don't think there's documentation on the distinction between using .src or .value or leaving the if statement without further qualifying the object you're referencing. We are definitely looking to expand upon our documentation and make things a bit more explicit when it comes to HubL functions, delimiters and syntax. HubL, though, is based loosely off Jinja2. If you check out their documentation, this can clarify a bit more as to how if statements can work: http://jinja.pocoo.org/docs/2.10/templates/#if

The way I think about it is, if I want to check to see if a certain aspect of a field in a custom module exists, I'll use .src or .value. For example, if I don't just want to check if an image is present, but actually want to check if the alt attribute on that image is present, I'll write something like this:

{% if module.image.alt %}
   <p>{{ module.image.alt }}</p>
{% endif %}

If I just want to check if the image field itself exists/has data, I wouldn't qualify it further with .alt:

{% if module.image %}
   <p>{{ module.image.alt }}</p>
{% endif %}

The second example might not be a great choice though, just in case the image actually doesn't have any alt text associated with it.

One thing that you can really use to your advantage is the Developer Info if you are confused about which object/property on an object you should be using: https://designers.hubspot.com/docs/hubl/how-to-use-developer-info-on-cos-pages.

If I'm unsure of what I should be referencing, I'll check the developer info for the object and the particular property on that object that i'd want to print out or do something to, which helps immensely when seeing if I should be referencing something one way or another.

Reply
0 Upvotes
5 Replies 5
HubSpot Moderator

Hi @jau, all HubL related questions should go into the COS/Content category. I've moved it over to that category for you.

As for your question, for a rich text module, you should be able to have the following which would accomplish what you're looking for:

{% if module.heading %}
   DO THING
{% endif %} 

The conditional statements themselves haven't changed in the new DM, but the reference of widget_data / widget / module depends on which context you're in. widget vs module should be exactly the same, but widget_data is usually used if you're exporting any values to the template context. If you share exactly what you're looking to do, I may be able to advise further. Just send over a link to the page you're on and a description of your goal and I'll do my best!

Reply
0 Upvotes
jau
Regular Contributor

C,

That works! If you don't mind me asking where did you find that documented? I'm referencing this page:

If statements

HubL uses if statements to help define the logic of a template. The syntax of HubL if statements is very similar to conditional logic in Python. If statements are wrapped in statement delimiters, starting with an opening if statement and ending with...

I don't see any information on if statements in regards to fields within a module. That page covers a handful of other scenarios. Now that you explain it, it makes sense to me, if within module there is heading makes sense. Where i'm getting lost is trying to find the correct documentation to use this language properly(So i can not bother you guys on here! And keep my projects moving forward).

My use of src and value above was more out of desperation than logic. Having tried to utilize the information available and not succeeding.

Thanks again for your help I really appreciate it.

Reply
0 Upvotes
HubSpot Moderator

Hi @jau, I don't think there's documentation on the distinction between using .src or .value or leaving the if statement without further qualifying the object you're referencing. We are definitely looking to expand upon our documentation and make things a bit more explicit when it comes to HubL functions, delimiters and syntax. HubL, though, is based loosely off Jinja2. If you check out their documentation, this can clarify a bit more as to how if statements can work: http://jinja.pocoo.org/docs/2.10/templates/#if

The way I think about it is, if I want to check to see if a certain aspect of a field in a custom module exists, I'll use .src or .value. For example, if I don't just want to check if an image is present, but actually want to check if the alt attribute on that image is present, I'll write something like this:

{% if module.image.alt %}
   <p>{{ module.image.alt }}</p>
{% endif %}

If I just want to check if the image field itself exists/has data, I wouldn't qualify it further with .alt:

{% if module.image %}
   <p>{{ module.image.alt }}</p>
{% endif %}

The second example might not be a great choice though, just in case the image actually doesn't have any alt text associated with it.

One thing that you can really use to your advantage is the Developer Info if you are confused about which object/property on an object you should be using: https://designers.hubspot.com/docs/hubl/how-to-use-developer-info-on-cos-pages.

If I'm unsure of what I should be referencing, I'll check the developer info for the object and the particular property on that object that i'd want to print out or do something to, which helps immensely when seeing if I should be referencing something one way or another.

Reply
0 Upvotes
Highlighted
jau
Regular Contributor

C,

Thanks so much for all the info! I'll have a lot of reading to do this weekend, I had read briefly about jinja being the base for a lot of this at one point, hadn't thought about using developer tools though.

Seriously thank you!

HubSpot Moderator

@jau no problem! Glad I could help :slight_smile: have a great weekend.

Reply
0 Upvotes