Skip to content

Instantly share code, notes, and snippets.

@hazho
Created August 16, 2022 15:19
Show Gist options
  • Save hazho/ef9d6992893c46a3a3acf9b2667719e7 to your computer and use it in GitHub Desktop.
Save hazho/ef9d6992893c46a3a3acf9b2667719e7 to your computer and use it in GitHub Desktop.
Hierarchic Custom Pages Menu
'''
other blocks ...
'''
class CustomPagesMenuBlock(StructBlock):
menus=ListBlock(StructBlock([
("pg", PageChooserBlock(default=None, blank=True, null=True, required=False, label="Menu", help = "height for this video embed object")),
("children", ListBlock(StructBlock([
("pg", PageChooserBlock(default=None, blank=True, null=True, required=False, label="Sub-Menu")),
("children", ListBlock(StructBlock([
("pg", PageChooserBlock(default=None, blank=True, null=True, required=False, label="Sub-Sub-Menu")),
])))
])))
]))
class Meta:
icon = ""
template = "blocks/custom_pages_menu_block.html"
'''
other blocks ...
'''
<ul class="custom_nav">
{% for menu in self.menus %}
<li class="top_menu">
{% if not menu.children %}
<li class="top_menu_item"><a href="{{menu.pg.url}}" class="pg_lin">{{ menu.pg }}</a></li>
{% else %}
<li class="top_menu_item has_sub_pgs"><a href="{{menu.pg.url}}" class="pg_lin">{{ menu.pg }}</a>
<ul class="sub_pgs">
{% for spg in menu.children %}
{% if not spg.children %}
{% if spg.pg %} <li class="sub_page"><a href="{{spg.pg.url}}" class="pg_lin">{{spg.pg}}</a></li> {% endif %}
{% else %}
{% if spg.pg %} <li class="top_menu_item has_sub_pgs"><a href="{{spg.pg.url}}" class="pg_lin">{{ spg.pg }}</a> {% endif %}
<ul class="sub_pgs">
{% for sspg in spg.children %}
{% if sspg.pg %}<li class="sub_page"><a href="{{sspg.pg.url}}" class="pg_lin">{{sspg.pg}}</a></li>{% endif %}
{% endfor %}
</ul>
{% endif %}
{% endfor %}
</ul>
</li>
{% endif %}
</li>
{% endfor %}
</ul>
@hazho
Copy link
Author

hazho commented Aug 16, 2022

though the block is implemented inside an instance of pages model, however, this implementation can be applied to siteSettings as well, in a way you can call the instances of each single site publicly in the base.html sections other than the specific page streamfield sections

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment