Skip to content

Commit

Permalink
Load sidebar templates dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
smanga24 committed Dec 18, 2024
1 parent 86d8b73 commit cc92dfb
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 24 deletions.
7 changes: 5 additions & 2 deletions src/content/templates/content/content_page.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "dwds_content.html" %}
{% load wagtailcore_tags bookmarks %}
{% load feedback %}
{% load template_renderer %}

{% block primary_content %}
{% if attribution %}
Expand All @@ -23,5 +23,8 @@
{% endblock post_primary_content %}

{% block secondary_content %}
{% feedback_sidebar %}
{% render_dynamic_templates page_type="secondary_page" as secondary_page_templates %}
{% for template in secondary_page_templates %}
{% include template.template_name with title=template.title description=template.description %}
{% endfor %}
{% endblock secondary_content %}
14 changes: 0 additions & 14 deletions src/core/templatetags/feedback.py

This file was deleted.

22 changes: 22 additions & 0 deletions src/core/templatetags/template_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django import template

from .templates_config import TEMPLATE_SECTIONS


register = template.Library()


@register.simple_tag(takes_context=True)
def render_dynamic_templates(context, page_type):
matching_section = next(
(section for section in TEMPLATE_SECTIONS if section["page_type"] == page_type),
None,
)
if matching_section is None:
return []

return [
action["render"]()
for action in matching_section.get("actions", [])
if action["is_visible"](context)
]
33 changes: 33 additions & 0 deletions src/core/templatetags/templates_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
FEEDBACK = "feedback"

TEMPLATES = {
FEEDBACK: {
"title": "Give feedback",
"description": "Did you find what you were looking for?",
"template_name": "dwds/components/feedback.html",
},
}

TEMPLATE_SECTIONS = [
{"page_type": "primary_page", "actions": []},
{
"page_type": "secondary_page",
"actions": [
{
"action_type": FEEDBACK,
"is_visible": lambda context: is_visible(context, FEEDBACK),
"render": lambda: render_template(FEEDBACK),
},
],
},
]


def is_visible(context, action_type):
if action_type == FEEDBACK:
return context.get("USER_IS_AUTHENTICATED", False)
return False


def render_template(action_type):
return TEMPLATES.get(action_type, {})
7 changes: 5 additions & 2 deletions src/home/templates/home/home_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% load wagtailimages_tags %}
{% load site_alert %}
{% load cards %}
{% load feedback %}
{% load template_renderer %}

{% block pre_main %}
{{ block.super }}
Expand Down Expand Up @@ -94,7 +94,10 @@
{% include "dwds/components/link_list.html" with title="Quick links" list=quick_links %}
</div>
{% endif %}
{% feedback_sidebar %}
{% render_dynamic_templates page_type="secondary_page" as secondary_page_templates %}
{% for template in secondary_page_templates %}
{% include template.template_name with title=template.title description=template.description %}
{% endfor %}
</section>
</article>
{% endblock secondary_content %}
7 changes: 5 additions & 2 deletions src/news/templates/news/news_home.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% load wagtailcore_tags %}
{% load wagtailimages_tags %}
{% load cards %}
{% load feedback %}
{% load template_renderer %}

{% block bookmark %}
{% endblock bookmark %}
Expand All @@ -19,5 +19,8 @@

{% block secondary_content %}
{% comment %} {% include "news/includes/news_categories.html" %} {% endcomment %}
{% feedback_sidebar %}
{% render_dynamic_templates page_type="secondary_page" as secondary_page_templates %}
{% for template in secondary_page_templates %}
{% include template.template_name with title=template.title description=template.description %}
{% endfor %}
{% endblock secondary_content %}
7 changes: 5 additions & 2 deletions src/peoplefinder/templates/peoplefinder/team.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "dwds_content.html" %}

{% load markdown %}
{% load feedback %}
{% load template_renderer %}

{% block title %}
{{ page_title }}
Expand Down Expand Up @@ -110,5 +110,8 @@ <h2>People in {{ team.short_name }}</h2>
{% endblock primary_content %}

{% block secondary_content %}
{% feedback_sidebar %}
{% render_dynamic_templates page_type="secondary_page" as secondary_page_templates %}
{% for template in secondary_page_templates %}
{% include template.template_name with title=template.title description=template.description %}
{% endfor %}
{% endblock secondary_content %}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "dwds_content.html" %}
{% load wagtailcore_tags %}
{% load feedback %}
{% load template_renderer %}

{% block bookmark %}
{% endblock bookmark %}
Expand Down Expand Up @@ -39,5 +39,8 @@ <h2 class="govuk-accordion__section-heading">
{% endblock %}

{% block secondary_content %}
{% feedback_sidebar %}
{% render_dynamic_templates page_type="secondary_page" as secondary_page_templates %}
{% for template in secondary_page_templates %}
{% include template.template_name with title=template.title description=template.description %}
{% endfor %}
{% endblock secondary_content %}

0 comments on commit cc92dfb

Please sign in to comment.