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: HTML+HubLテンプレート - HubSpot docs
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.
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!
{% 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.
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.