# Printing child links using the menu function

**URL:** <https://community.hubspot.com/t/printing-child-links-using-the-menu-function/24585>\
**Category:** CMS Development\
**Tags:** design-manager\
**Created:** [August 25, 2020, 9:19am UTC](https://community.hubspot.com/t/printing-child-links-using-the-menu-function/24585 "2020-08-25T09:19:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amit\_95](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/amit_95/32/74736_2.png) [@Amit\_95](https://community.hubspot.com/u/Amit_95)\
**Post date:** [August 25, 2020, 9:19am UTC](https://community.hubspot.com/t/printing-child-links-using-the-menu-function/24585/1 "2020-08-25T09:19:46Z")

</div>

I have a menu in the HubSpot backend configured like this:

 ![30671i9FF0F7ABA9C05589.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/8/d/8dd64934263bb42a363094800401d32a0ac13974.png)

I’m trying to use the [menu function](https://developers.hubspot.com/docs/cms/hubl/tags#menu) to print out the markup of the above menu.

At the moment, I have:

```lia-code-sample
{% set menu = menu(module.menu) %}
{% for item in menu.children %}
 <li class="{{ item.label|lower|replace(' ', '-') }}">
 <a href="{{ item.url }}">{{ item.label }}</a>
 </li>
{% endfor %}

```

But the above only prints the markup for top level links, not the child links underneath them. So it prints the following markup:

```lia-code-sample
<li class="get-connected">
 <a href="#">Get connected</a>
</li>

<li class="register">
 <a href="#">Register</a>
</li>

<li class="tracks">
 <a href="#">Tracks</a>
</li>

```

How can I get it so that it prints the child markup too? So, what I am looking for is this (example):

```lia-code-sample
<li class="get-connected">
 <a href="#">Get connected</a>
 <ul>
 <li>FF Studio</li>
 <li>FF Blog</li>
 </ul>
</li>

```

---

<div class="post-metadata">

**Author:** ![Anton](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/anton/32/103167_2.png) [@Anton](https://community.hubspot.com/u/Anton)\
**Post date:** [August 25, 2020, 5:34pm UTC](https://community.hubspot.com/t/printing-child-links-using-the-menu-function/24585/2 "2020-08-25T17:34:51Z")

</div>

Hi [@Amit\_95](https://community.hubspot.com/t5/user/viewprofilepage/user-id/64311),

you don’t need a for loop to display the menu if you’re using this hubl code.

Simply paste it in your module/template and leave the id blank. When building a page you can select the desired menu. HubSpot will automaticly translate the hubl into the right menu structure.

Tipp: The ID is a bit hidden but can be found after selecting the menu once and previewing the page. When you inspect the sourcecode of the page/menu you should see this line(s):

![30695i19E7AAC9FB81F0D3.png](https://d2qpr2jzcazej7.cloudfront.net/optimized/3X/0/0/006105e9ee651f944ce6f5666e2ca29aa68d66f6_2_690x30.png)  
The “data-menu-id” is your menu ID. So after you found it you can go back to your module/template and paste it in. By doing this you won’t need to select the menu everytime you’re building a page.

best,

Anton

---

<div class="post-metadata">

**Author:** ![Amit\_95](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/amit_95/32/74736_2.png) [@Amit\_95](https://community.hubspot.com/u/Amit_95)\
**Post date:** [August 26, 2020, 8:48am UTC](https://community.hubspot.com/t/printing-child-links-using-the-menu-function/24585/3 "2020-08-26T08:48:49Z")

</div>

Hi @Anton,

The reason I’m running the for loop is so that I can have control over the menu’s markup (don’t want those spans).

The [docs for the menu function I’m using is here](https://developers.hubspot.com/docs/cms/hubl/functions#menu). However, in the docs, it doens’t say how I can print child items?

---

<div class="post-metadata">

**Author:** ![Anton](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/anton/32/103167_2.png) [@Anton](https://community.hubspot.com/u/Anton)\
**Post date:** [August 26, 2020, 9:24am UTC](https://community.hubspot.com/t/printing-child-links-using-the-menu-function/24585/4 "2020-08-26T09:24:26Z")

</div>

If you want to get rid of the spans, simply add “no\_wrapper=True” to your menu hubl code.  
It should look like this:  
{% menu “my\_menu” id=456, flyouts=‘true’, max\_levels=‘2’, flow=‘horizontal’, label=‘Advanced Menu’, no\_wrapper=True %}  
Best,  
Anton

---

<div class="post-metadata">

**Author:** ![Amit\_95](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/amit_95/32/74736_2.png) [@Amit\_95](https://community.hubspot.com/u/Amit_95)\
**Post date:** [August 26, 2020, 11:54am UTC](https://community.hubspot.com/t/printing-child-links-using-the-menu-function/24585/5 "2020-08-26T11:54:14Z")

</div>

Hi @Anton

It’s not just the span I want to remove, it’s the unncessary classes and data attributes that come alongside this appoach too. Whilst your approach does the job, I’m looking to be in complete control of the markup that is generated within my list items.

Are you suggesting there is no way for me achieve this with the [partial approach](https://developers.hubspot.com/docs/cms/hubl/functions#menu) HubSpot have defined here?

---

<div class="post-metadata">

**Author:** ![Anton](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/anton/32/103167_2.png) [@Anton](https://community.hubspot.com/u/Anton)\
**Post date:** [August 26, 2020, 6:05pm UTC](https://community.hubspot.com/t/printing-child-links-using-the-menu-function/24585/6 "2020-08-26T18:05:34Z")

</div>

Hi [@Amit\_95](https://community.hubspot.com/t5/user/viewprofilepage/user-id/64311),

if you want to work with this code it’s fine. I’ve modified you the code:

```lia-code-sample
{% set default_node = menu("default") %}

<ul>
 {% for child in default_node.children %}
{% if child.level == 1 %}
 <li><a href="{{ child.url }}">{{ child.label }}</a></li>
{% else %}
 <li>
 <ul>
 <li><a href="{{ child.url }}">{{ child.label }}</a></li>
 </ul>
 </li>
{% endif %}
{% endfor %}
</ul>

```

But if you want to completly modify it and have complete control over your menu try this:

Write your own menu Module

It’s easier than it sounds.

Here’s my code:

```lia-code-sample
{# Config #}
{% for item in module.single_nav_entry %}{% set href = item.menu_url.url.href %}
{% if item.menu_url.url.type is equalto "EMAIL_ADDRESS" %}
{% set href = "mailto:" + href %}
{% endif %}
{% endfor %}

{# Code #}
<ul>
 {% for item in single_nav_entry %}
 {% if item.child %}
 <li>
 <ul>
 <li class="{{item.custom_class}}"><a href="{{ href }}"{% if item.menu_url.open_in_new_tab %}target="_blank"{% endif %}{% if item.menu_url.no_follow %}rel="nofollow"{% endif %}>{{ item.menu_name }}</a></li>
 </ul>
 </li>
 {% else %}
 <li class="{{item.custom_class}}"><a href="{{ href }}"{% if item.menu_url.open_in_new_tab %}target="_blank"{% endif %}{% if item.menu_url.no_follow %}rel="nofollow"{% endif %}>{{ item.menu_name }}</a></li>
 {% endif %}
 {% endfor %}
</ul>

```

and here some screens of the module functions/widgets:

 ![30732i1B11D70B886ECB56.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/f/a/faef21ca7a760dc02c8fcfb54c5aecbc22e4f012.png)  
module widgets  
 ![30733i1219137898E05A14.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/7/3/7332fd325c6474ffe078e6a6f89b5b77c4fa0634.png)  
group settings

If you want to use it as a global element don’t forget to select “global module” when creating the module. If you want to use the module in a global group you don’t need to do this.

best,

Anton

---

<div class="post-metadata">

**Author:** ![MPeric](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mperic/32/89259_2.png) [@MPeric](https://community.hubspot.com/u/MPeric)\
**Post date:** [May 11, 2022, 1:54pm UTC](https://community.hubspot.com/t/printing-child-links-using-the-menu-function/24585/7 "2022-05-11T13:54:12Z")

</div>

Just to piggyback on this since this is not a working solution and I didn’t find a lot of useful info online.  
Regarding this:

```lia-code-sample
{% set default_node = menu("default") %}
<ul>
 {% for child in default_node.children %}
 {% if child.level == 1 %}
 <li><a href="{{ child.url }}">{{ child.label }}</a></li>
 {% else %}
 <li>
 <ul>
 <li><a href="{{ child.url }}">{{ child.label }}</a></li>
 </ul>
 </li>
 {% endif %}
 {% endfor %}
</ul>

```

This will not generate the child elements. It will yield the same results as the code from the original question (the code is basically the same with one extra condition added which does not affect the output).  
Here is the code from the original question:

```lia-code-sample
{% set menu = menu(module.menu) %}
{% for item in menu.children %}
 <li class="{{ item.label|lower|replace(' ', '-') }}">
 <a href="{{ item.url }}">{{ item.label }}</a>
 </li>
{% endfor %}

```

However, after exploring the output of menu fuction, here is the working solution with macro included:

```lia-code-sample
{% macro generateMenuMarkup(menuId) %}
 {% set menu = menu(menuId) %}
 {% for child in menu.children %}
 <h2>{{child.label}}</h2>
 <ul>
 {% for node in child.children %}
 <li>{{node.label}}</li>
 {% endfor %}
 </ul>
 {% endfor %}
{% endmacro %}

```

Calling this macro is simple as **{{ generateMenuMarkup(module.menu) }}**

Calling the macro with the following menu:

 ![MPeric_0-1652275586929.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/c/5/c5e4727b955471f5f6df25a739c46b08be73ffa5.png)

Will yield following markup:

```lia-code-sample
<h2>LINK-1</h2>
<ul>
 <li>LINK-1-1</li>
 <li>LINK-1-2</li>
 <li>LINK-1-3</li>
</ul>
<h2>LINK-2</h2>
<ul>
 <li>LINK-2-1</li>
 <li>LINK-2-2</li>
 <li>LINK-2-3</li>
</ul>

```

This way, you have complete control over markup regarding menus.  
One additional benefit is that you can use hubl tokens directly in your menu and they will be properly rendered in your hubl markup which is not the case when you include menu directly with hubl snippet:

**{% menu id=“{{ module.user\_logged\_in\_menu }}” %}**

Example:

 ![MPeric_1-1652276562649.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/2/b/2bdec8a85365d11db0661df094cad4d99feadb54.png)

When using macro and menu() function the **content.absolute\_url value ([https://www.somelink.com](https://www.somelink.com))** will be rendered.  
When using hubl menu snippet, **{{ content.absolute\_url }}** will be rendered as a simple string and value will not be inserted.

**NOTE:** This code will work for menus which are 1 level deep. For deeper menus more for loops will be needed.

Kind regards,  
Milos
