CMS Development

JamesSilver
Participant

Is it possible to write (or simulate) custom functions in hubl?

I can see it is possible to implement custom filters in Jinja (which inspires hubl) but this requires custom python code, and I'm not sure what the equivalent would be in the context of Hubspot.

 

I can see there are macros, but a macro seems tied to the idea that it always outputs markup directly. I can't see an obvious way it could return a value back to the caller.

 

Simulating with Macros

I have managed to simulate a function using a macro with some success. For example:

 

 

{% macro fn_sum(a, b, _r) %}
  {% do _r.update({
    "return": a + b,
  }) %}
{% endmacro %}

 

 

It passes its return value back up by reference via an object argument.

 

It is invoked as follows:

 

 

{% set _r = {} %}
{{- fn_sum(3, 5, _r) -}}
{% set sum = _r.return %}

 

 

I have this sort of code live and working, but I confess it's pretty confusing to read. I'm also concerned because I've just started seeing some strange behaviour. It's on a blog post page, and when the page is served normally, it's as if the "function" was never called. When I lift caching (via "?hsDebug=true") it works correctly.

 

How are macros cached/optimized by Hubspot? If it never outputs a value, might Hubspot sometimes choose to skip its execution?

 

Am I missing a better way of extending Hubspot?

 

Background / Use case

To provide more context, my use-case is this: we have a multilingual site, and we have a "strings.html" template that defines a dictionary of all the English strings that would otherwise be hard-coded in a template or a module. strings.html transparently takes care of swapping keys out for the translated strings, modules lookup a string by key.

 

Some of these translatable strings define links, and each link is actually a group of related strings with consistent prefixes. Consider these:

 

 

strings.global_link_1__url
strings.global_link_1__title
strings.global_link_1__open_in_new_tab
strings.global_link_1__text
strings.global_link_2__url
strings.global_link_2__title
strings.global_link_2__open_in_new_tab
strings.global_link_2__text
strings.global_link_3__url
strings.global_link_3__title
strings.global_link_3__open_in_new_tab
strings.global_link_3__text

 

 

I render these same links in several different templates, but with slightly different markup each time (e.g. header, footer variation 1, footer variation 2, etc).

 

I'd prefer to include the actual markup for the links directly in the particular template, as it is specific to that template, so I'd rather not have a shared macro that changes its output based on an arbitrary "variation" parameter.

 

I'd prefer to have the code that "parses" the strings dictionary and creates an array of links in one place, and not have it duplicated in all three templates. Perhaps, for example, one day we add support for three more links "strings.global_link_4_*", "strings.global_link_5_*", "strings.global_link_6_*". Now I only have to update this in one place.

 

Ideally I want a function I can use like this:

 

 

{% for link in fn_global_links() %}
  <a href="{{ link.href }}"
     {% if link.open_in_new_tab %}target="_blank"{% endif %}
     >
    {{ link.text }}
  </a>
{% endfor %}

 

 

 

0 Upvotes
4 Replies 4
tominal
Guide | Partner
Guide | Partner

Is it possible to write (or simulate) custom functions in hubl?

Hey @JamesSilver,

 

I apologize in advance if I am missing the question here. There is a lot of possibilities here.

 

Are you able to store the HTML in variables? Also, if you stored the links in a separate file, you can import the code, then loop through the array of values.

 

What if you imported the translations from a separate file?

 

{% from '../imports/links.html' import LINKS as links %}

 

 In that translations.html, you could define a JSON object that contains variations.

{% set LINKS = { "types": [] } %}

 

Then, you could loop through it using a HubL loop.

{% for link in links["types"] %}{% endfor %}

 

Again, apologies if I missing the mark here. I know there's a way to solve your use case here.

 

Hope that helps!


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com
0 Upvotes
JamesSilver
Participant

Is it possible to write (or simulate) custom functions in hubl?

Hi Thomas, thank you for engaging with my question.

 

Yes, that's a nice simple idea to take the links from a separate file and have them already defined in the right structure. I guess I've avoided that because we consider our "strings.html" file to be CMSable--the content admins do go into Design Tools and change the strings there--and I wanted to keep it all in one place for them.

 

 

tominal
Guide | Partner
Guide | Partner

Is it possible to write (or simulate) custom functions in hubl?

Would you mind sharing the syntax of your strings.html file? I think you can keep it all in one place. Perhaps a different format of data would help out here.


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com
Jaycee_Lewis
Community Manager
Community Manager

Is it possible to write (or simulate) custom functions in hubl?

Hi, @JamesSilver 👋 This is an interesting question. Thanks for taking the time to include all those details. I'm going to tag some community members and see if we can get a conversation going.

 

Hey, @Phil_Vallender @nikodev @JBeatty @CMcKay @weiliang_lee @tominal have you ever ventured down this road? Thank you very much for taking a look! — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot