Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to list children automatically #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion bmrc/templates/standard/standard_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ <h1>{{self.title}}</h1>
{% for block in page.body %}
{% include_block block %}
{% endfor %}

{% if page.show_nested_children and page.get_children.live.in_menu.exists %}
{% include "standard/includes/child_pages_listing.html" %}
{% endif %}
</div>

{% if self.sidebar.all %}
Expand All @@ -31,4 +35,4 @@ <h2>{{ item_set.sidebar_title }}</h2>

</div> <!-- // END container -->

{% endblock %}
{% endblock %}
2 changes: 1 addition & 1 deletion home/fixtures/dev.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.0.10 on 2025-02-05 22:11

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("standard", "0013_alter_standardpage_body"),
]

operations = [
migrations.AddField(
model_name="standardpage",
name="nested_children_depth",
field=models.PositiveIntegerField(
default=1,
help_text="Number of levels deep to show child pages (1 means immediate children only).",
),
),
migrations.AddField(
model_name="standardpage",
name="show_nested_children",
field=models.BooleanField(
default=False, help_text="Check this to display nested child pages."
),
),
]
16 changes: 16 additions & 0 deletions standard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,28 @@ class StandardPage(Page):
blank=True,
)

show_nested_children = models.BooleanField(
default=False, help_text="Check this to display nested child pages."
)

nested_children_depth = models.PositiveIntegerField(
default=1,
help_text="Number of levels deep to show child pages (1 means immediate children only).",
)

content_panels = Page.content_panels + [
FieldPanel("body"),
MultiFieldPanel(
[InlinePanel("sidebar", max_num=3, label="Sidebar Section")],
heading="Sidebar",
),
MultiFieldPanel(
[
FieldPanel('show_nested_children'),
FieldPanel('nested_children_depth'),
],
heading='Dynamic Page Listing',
),
]

class Meta:
Expand Down
9 changes: 9 additions & 0 deletions standard/templates/standard/includes/child_pages_listing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% if page.show_nested_children and page.get_children.live.in_menu.exists %}
{% load basic_tags %}
<nav class="container-sm pb-3 pb-lg-5">
<h2>Further Reading</h2>
<ul class="nav flex-column">
{% render_nested_pages page page.nested_children_depth %}
</ul>
</nav>
{% endif %}
26 changes: 26 additions & 0 deletions standard/templatetags/basic_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django import template
from django.utils.safestring import mark_safe

register = template.Library()


@register.simple_tag(takes_context=True)
def render_nested_pages(context, page, max_depth=1, current_depth=1):
"""Recursive template tag for displaying a sitemap for a section
of the site."""
if current_depth > max_depth:
return ''

output = []
for child in page.get_children().specific():
if child.live and child.show_in_menus:
output.append(f'<li class="nav-item"><a class="nav-link" href="{child.url}">{child.title}</a>')
if child.get_children().filter(live=True, show_in_menus=True).exists():
output.append('<ul class="nav flex-column">')
output.append(
render_nested_pages(context, child, max_depth, current_depth + 1)
)
output.append('</ul>')
output.append('</li>')

return mark_safe('\n'.join(output))