CMS Development

FedeColap
Contributor

Loop through a group of modules

Hello Community, 

I am reaching out for help, please. I wan to create a module (a sidebar) that loops through a group of modules and renders out the title of each one of them (like a summary). In my specific case,

1) the sidebar is in the same custom template of the group of modules, and

2) the modules grouped are just Rich Text, with a title and a content. 

 

Screen Shot 2020-04-29 at 12.48.54 PM.pngScreen Shot 2020-04-29 at 12.51.51 PM.png

My question is, how do I access inside the group of moduels, so that I can loop through it from the external module? I have not found any id for that group that I can use as a variable (but I am new to this, so maybe I didn't look in the right place). 

Would placing the external module inside the group be of any help? My idea was to keep it "independent" so that it can be used in other pages.

 

Any help is greatly appreciated! 

0 Upvotes
3 Replies 3
IsaacTakushi
HubSpot Employee
HubSpot Employee

Loop through a group of modules

Hi, @FedeColap.

 

Apologies for the delayed response. I'll be looking into this. Thanks for your patience.

Isaac Takushi

Associate Certification Manager
0 Upvotes
FedeColap
Contributor

Loop through a group of modules

Hi @IsaacTakushi , 

 

no worries. In the meantime I Have reached out to HubSpot and got confirmation that given a Module that we can call "external", it cannot loop through a Group of Modules. It is possible for a module to loop through itself, but not loop through an external group of modules. 

 

I ended up creating a huge module that handles everything - specifically, I have treated this module as a "template" and each "section" of the module as a "module" itself, but I admit this is not very agile. 

 

So I assume grouping modules is basically only for styling. Feel free to investigate on your end, or contribute proposing other alternatives that I couldn't think of, otherwise... I think I can solve it. Hope this helps! 

 

FedeColap

0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

Loop through a group of modules

Hi, @FedeColap.

 

I'm glad you found a path forward, even if it wasn't the one you envisioned.


Yes, the grouping shown in your first screenshot facilitates styling over HubL functionality.

 

I actually began investigating by recreating your group of custom modules as a single custom module containing a repeater group.

 

I then added the HubL logic below to a coded template to loop through each title field in the custom module's groups:

 

<ul>
{# The field group below is a repeater field, so it returns a dict. We wish to look inside content.widgets.module_1234567890.body.field_group and loop through its children. Because these are the children of that field group, I have given it the arbitrary name 'group_instance'. #} {% for group_instance in content.widgets.module_1234567890.body.field_group %} <li>{{group_instance.title}}</li> {% endfor %} </ul>

I was not able to achieve this with another custom module on a drag-and-drop template, however, as custom modules cannot reference each others' fields. Variables and fields are only accessible within each module or on the template level.

Isaac Takushi

Associate Certification Manager
0 Upvotes