When the custom module is rendered, an unique id is attached to the HTML div component with the format hs_cos_wrapper_widget_1538643526593
Is there a way to get this unique ID in the module itself through HUBL?
Basically I’m looking for a way to identify the very instance of the module in template to assign media-query specific CSS. Class alone is not unique enough for our use-case.
Hey @yidanwang I believe that you can access that using the {{ name }} variable in HubL within the custom module. Here is an example where I used the variable in my code:
Please note that HubL can only be used in the HTML pane of the custom module editor so you might need to put your CSS in some <style> tags in the HTML pane and wrap that in {% require_css %}{% end_require_css %}
Hey @kchung would you mind sending over a screenshot of your code? Are you trying to have a unique id/class usign {{ name }} for each module instance (e.g. an image module is added to a page three times) or for a module field in a for loop (e.g. a tabber with repeatable tabs)?
Hey @kchung thanks for sending that example! So {{ name }} would be individual to the module as a whole and not a field within a repeater of the module. For example if you added {{ name }} to the top level class on your module (on line 1), each time you added that module to a page the module you added would have a unique class name. If you wanted to have a unique class name for each item in a loop I’d recommend {{ loop.index }} (For Loops - HubSpot docs). For example:
{% for item in forloop %}
<div id=“story-item-{{ loop.index }}”>
</div>
{% endif %}
This should output a number after “story-item-” for the ID which should keep each loop item unique.