CMS Development

TMisuna
Contributor

How do I write conditional logic based on templateType?

SOLVE

I’m trying to work out how to add conditional logic based on templateType in HubSpot CMS. I’ve already looked at the documentation here: https://developers.hubspot.jp/docs/guides/cms/content/templates/types/html-hubl-templates

 

Inside the global base.html file, I need to inject a structured-data breadcrumb script in the head element, but only for certain template types.

I was wondering if something like the snippet below would work:

{% if templateType == "page" %}
  <script>
    console.log("page");
  </script>
{% elif templateType == "blog_post" %}
  <script>
    console.log("blog_post");
  </script>
{% endif %}

 

I haven’t found any official examples, so I’m unsure whether this approach is valid. In WordPress we can use flexible conditionals for each template; does HubSpot offer anything similar?

 

Additionally, if the templateType is blog_post, is there a way to add another layer of conditional logic that targets only a specific blog by its ID?

 

If anyone knows how to handle these conditionals, I’d appreciate your advice.

0 Upvotes
2 Accepted solutions
evaldas
Solution
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

How do I write conditional logic based on templateType?

SOLVE

Hi @TMisuna,

 

You could utilize the builtin_body_classes variable. HubSpot automatically adds classes to the <body> based on the content type.

 

For example:

  • Page would have .hs-site-page 
  • Blog listing would have .hs-blog-listing 

 

So you could use conditional logic based on those. 

 

However, if you need even more granular control, you could try setting a variable in the template itself

 

{% set template_var = "page_template_type1" %}

 

Regarding the blog IDs, yes you can create rules for that by using the group.id blog variable.

 

Hope this helps!

✔️ Did this post help answer your query? Help the community by marking it as a solution.

View solution in original post

evaldas
Solution
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

How do I write conditional logic based on templateType?

SOLVE

Glad to hear that worked for you!

 

One additional tip - if your theme starts having more page templates in the future, instead of adding each individual template to the "if" statement, you could target all site pages with this logic:

 

{% if builtin_body_classes is string_containing "hs-site-page" %} 

 

This would apply to any Website Page, regardless of the template. 

 

✔️ Did this post help answer your query? Help the community by marking it as a solution.

View solution in original post

3 Replies 3
evaldas
Solution
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

How do I write conditional logic based on templateType?

SOLVE

Hi @TMisuna,

 

You could utilize the builtin_body_classes variable. HubSpot automatically adds classes to the <body> based on the content type.

 

For example:

  • Page would have .hs-site-page 
  • Blog listing would have .hs-blog-listing 

 

So you could use conditional logic based on those. 

 

However, if you need even more granular control, you could try setting a variable in the template itself

 

{% set template_var = "page_template_type1" %}

 

Regarding the blog IDs, yes you can create rules for that by using the group.id blog variable.

 

Hope this helps!

✔️ Did this post help answer your query? Help the community by marking it as a solution.

TMisuna
Contributor

How do I write conditional logic based on templateType?

SOLVE
{% if template_meta.path == "theme-name/templates/page.html" %}

I was able to achieve what I wanted to do by using template_meta in the form of
Thank you very much.

evaldas
Solution
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

How do I write conditional logic based on templateType?

SOLVE

Glad to hear that worked for you!

 

One additional tip - if your theme starts having more page templates in the future, instead of adding each individual template to the "if" statement, you could target all site pages with this logic:

 

{% if builtin_body_classes is string_containing "hs-site-page" %} 

 

This would apply to any Website Page, regardless of the template. 

 

✔️ Did this post help answer your query? Help the community by marking it as a solution.