diff --git a/bakerydemo/base/jinja2tags.py b/bakerydemo/base/jinja2tags.py new file mode 100644 index 000000000..a381f9e72 --- /dev/null +++ b/bakerydemo/base/jinja2tags.py @@ -0,0 +1,59 @@ +from django.template.context_processors import csrf +from django.template.defaultfilters import ( + cut, + date, + linebreaks, + pluralize, + slugify, + truncatewords, + urlencode, +) +from django.contrib.staticfiles.storage import staticfiles_storage +from jinja2 import pass_context +from jinja2.ext import Extension + +from wagtail.contrib.search_promotions.templatetags.wagtailsearchpromotions_tags import ( + get_search_promotions, +) + +from bakerydemo.base.templatetags.navigation_tags import ( + breadcrumbs, + get_footer_text, + get_site_root, + top_menu, + top_menu_children, +) +from bakerydemo.base.templatetags.gallery_tags import gallery + + +class BaseExtension(Extension): + def __init__(self, environment): + super().__init__(environment) + self.environment.globals.update( + { + "static": staticfiles_storage.url, + "csrf": csrf, + "get_search_promotions": get_search_promotions, + "breadcrumbs": pass_context(breadcrumbs), + "get_footer_text": pass_context(get_footer_text), + "get_site_root": pass_context(get_site_root), + "top_menu": top_menu, + "top_menu_children": top_menu_children, + "gallery": gallery, + } + ) + + self.environment.filters.update( + { + "cut": cut, + "date": date, + "linebreaks": linebreaks, + "pluralize": pluralize, + "slugify": slugify, + "truncatewords": truncatewords, + "urlencode": urlencode, + } + ) + + +base = BaseExtension diff --git a/bakerydemo/base/templatetags/gallery_tags.py b/bakerydemo/base/templatetags/gallery_tags.py index 974f6a25b..cadb14dd5 100644 --- a/bakerydemo/base/templatetags/gallery_tags.py +++ b/bakerydemo/base/templatetags/gallery_tags.py @@ -1,15 +1,10 @@ -from django import template from wagtail.images.models import Image -register = template.Library() - # Retrieves a single gallery item and returns a gallery of images -@register.inclusion_tag("tags/gallery.html", takes_context=True) -def gallery(context, gallery): +def gallery(gallery): images = Image.objects.filter(collection=gallery) return { "images": images, - "request": context["request"], } diff --git a/bakerydemo/base/templatetags/navigation_tags.py b/bakerydemo/base/templatetags/navigation_tags.py index 75699dd26..c758c6602 100644 --- a/bakerydemo/base/templatetags/navigation_tags.py +++ b/bakerydemo/base/templatetags/navigation_tags.py @@ -1,13 +1,8 @@ -from django import template from wagtail.models import Page, Site from bakerydemo.base.models import FooterText -register = template.Library() -# https://docs.djangoproject.com/en/3.2/howto/custom-template-tags/ - -@register.simple_tag(takes_context=True) def get_site_root(context): # This returns a core.Page. The main menu needs to have the site.root_page # defined else will return an object attribute error ('str' object has no @@ -35,8 +30,7 @@ def is_active(page, current_page): # Retrieves the top menu items - the immediate children of the parent page # The has_menu_children method is necessary because the Foundation menu requires # a dropdown class to be applied to a parent -@register.inclusion_tag("tags/top_menu.html", takes_context=True) -def top_menu(context, parent, calling_page=None): +def top_menu(parent, calling_page=None): menuitems = parent.get_children().live().in_menu() for menuitem in menuitems: menuitem.show_dropdown = has_menu_children(menuitem) @@ -51,14 +45,11 @@ def top_menu(context, parent, calling_page=None): return { "calling_page": calling_page, "menuitems": menuitems, - # required by the pageurl tag that we want to use within this template - "request": context["request"], } # Retrieves the children of the top menu items for the drop downs -@register.inclusion_tag("tags/top_menu_children.html", takes_context=True) -def top_menu_children(context, parent, calling_page=None): +def top_menu_children(parent, calling_page=None): menuitems_children = parent.get_children() menuitems_children = menuitems_children.live().in_menu() for menuitem in menuitems_children: @@ -75,14 +66,11 @@ def top_menu_children(context, parent, calling_page=None): return { "parent": parent, "menuitems_children": menuitems_children, - # required by the pageurl tag that we want to use within this template - "request": context["request"], } -@register.inclusion_tag("tags/breadcrumbs.html", takes_context=True) def breadcrumbs(context): - self = context.get("self") + self = context.get("page") if self is None or self.depth <= 2: # When on the home page, displaying breadcrumbs is irrelevant. ancestors = () @@ -90,12 +78,11 @@ def breadcrumbs(context): ancestors = Page.objects.ancestor_of(self, inclusive=True).filter(depth__gt=1) return { "ancestors": ancestors, - "request": context["request"], } -@register.inclusion_tag("base/include/footer_text.html", takes_context=True) def get_footer_text(context): + # Use together with base/include/footer_text.html. # Get the footer text from the context if exists, # so that it's possible to pass a custom instance e.g. for previews # or page types that need a custom footer diff --git a/bakerydemo/blog/models.py b/bakerydemo/blog/models.py index 2f97ead6d..1d26747ab 100644 --- a/bakerydemo/blog/models.py +++ b/bakerydemo/blog/models.py @@ -188,7 +188,7 @@ def tag_archive(self, request, tag=None): return redirect(self.url) posts = self.get_posts(tag=tag) - context = {"self": self, "tag": tag, "posts": posts} + context = {"page": self, "tag": tag, "posts": posts} return render(request, "blog/blog_index_page.html", context) def serve_preview(self, request, mode_name): diff --git a/bakerydemo/jinja2/base.html b/bakerydemo/jinja2/base.html index 2bcd75dd4..240521116 100755 --- a/bakerydemo/jinja2/base.html +++ b/bakerydemo/jinja2/base.html @@ -1,4 +1,3 @@ -{% load navigation_tags static wagtailuserbar %}
@@ -12,7 +11,7 @@ {% endif %} {% endblock %} {% block title_suffix %} - | {{ settings.base.SiteSettings.title_suffix }} + | {{ settings("base.SiteSettings").title_suffix }} {% endblock %} @@ -23,21 +22,20 @@{{ page.hero_text }}
{% if page.hero_cta_link %} - + {{ page.hero_cta }} {% else %} @@ -30,15 +29,16 @@{{ object.job_title }}
{{ self.text }}
-{{ self.attribute_name}}
+diff --git a/bakerydemo/jinja2/blocks/embed_block.html b/bakerydemo/jinja2/blocks/embed_block.html index 257ea988e..94afb7557 100644 --- a/bakerydemo/jinja2/blocks/embed_block.html +++ b/bakerydemo/jinja2/blocks/embed_block.html @@ -1,3 +1 @@ -{% load wagtailimages_tags %} - -{{ self }} +{{ value|safe }} diff --git a/bakerydemo/jinja2/blocks/heading_block.html b/bakerydemo/jinja2/blocks/heading_block.html index a5ed6e65a..fa3092f05 100644 --- a/bakerydemo/jinja2/blocks/heading_block.html +++ b/bakerydemo/jinja2/blocks/heading_block.html @@ -1,15 +1,15 @@ -{% comment %} +{# Content is coming from the StandardBlock StreamField class within `blocks.py` -{% endcomment %} +#} -{% if self.size == 'h2' %} -{{ value.text }}
+{{ value.attribute_name}}
{{ self.heading_text }}
+{% if value.size == 'h2' %} +{{ value.heading_text }}
-{% elif self.size == 'h3' %} -{{ self.heading_text }}
+{% elif value.size == 'h3' %} +{{ value.heading_text }}
-{% elif self.size == 'h4' %} -{{ self.heading_text }}
+{% elif value.size == 'h4' %} +{{ value.heading_text }}
{% endif %} diff --git a/bakerydemo/jinja2/blocks/image_block.html b/bakerydemo/jinja2/blocks/image_block.html index a4b1badc4..31812c6fa 100644 --- a/bakerydemo/jinja2/blocks/image_block.html +++ b/bakerydemo/jinja2/blocks/image_block.html @@ -1,6 +1,4 @@ -{% load wagtailimages_tags %} - diff --git a/bakerydemo/jinja2/blocks/paragraph_block.html b/bakerydemo/jinja2/blocks/paragraph_block.html index e3125de36..40cf72a23 100644 --- a/bakerydemo/jinja2/blocks/paragraph_block.html +++ b/bakerydemo/jinja2/blocks/paragraph_block.html @@ -1 +1 @@ -{{ self }} +{{ value }} diff --git a/bakerydemo/jinja2/blocks/recipe_step_block.html b/bakerydemo/jinja2/blocks/recipe_step_block.html index 36d9a1cc9..19aa7e82e 100644 --- a/bakerydemo/jinja2/blocks/recipe_step_block.html +++ b/bakerydemo/jinja2/blocks/recipe_step_block.html @@ -1 +1 @@ -{{ self.text }} +{{ value.text }} diff --git a/bakerydemo/jinja2/blog/blog_index_page.html b/bakerydemo/jinja2/blog/blog_index_page.html index 8b69a05d9..1109725c0 100644 --- a/bakerydemo/jinja2/blog/blog_index_page.html +++ b/bakerydemo/jinja2/blog/blog_index_page.html @@ -1,5 +1,4 @@ {% extends "base.html" %} -{% load wagtailcore_tags navigation_tags wagtailimages_tags %} {% if tag %} {% block title %} @@ -26,10 +25,10 @@Blog
{{ blog.introduction|truncatewords:15 }}
+{{ blog.introduction|truncatewords(15) }}
{% endif %}{{ page.introduction|truncatewords:15 }}
+{{ page.introduction|truncatewords(15) }}
{% endif %}