Skip to content

Commit

Permalink
Replace the custom template tag with a decorator
Browse files Browse the repository at this point in the history
The decorator now adds a method to the dataclass that handles the rendering logic. This
is only done when no rendering method exists yet.

This allows for completely custom rendering logic to be added.

I find, this also reduces the logic and the complexity of the "templex" pattern, because
we can work with fewer moving pieces.
  • Loading branch information
tbrlpld committed Sep 9, 2023
1 parent f7f6e97 commit 9c7c84c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
1 change: 0 additions & 1 deletion lpld/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@
"libraries": {
"lpldutils": "lpld.templatetags.lpldutils",
"navigation": "lpld.templatetags.navigation",
"templex": "lpld.templatetags.templex",
},
},
},
Expand Down
45 changes: 41 additions & 4 deletions lpld/templates/molecules/teaser/teaser.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,51 @@
import dataclasses
from typing import Optional
import functools
from typing import Optional, Type

from django import template as django_template
from wagtail.images import models as images_models
from wagtailmedia import models as media_models


@dataclasses.dataclass
class Teaser:
template = "molecules/teaser/teaser.html"
def templex(template: str):
"""
Turn class into a templex object.
The class is turned into a data class and a `render` method is added (if it
doesn't already exist). Pass the template to render the templex with as a parameter
to the decorator.
"""

def wrapper(klass):
"""
The actual decorator.
The template that this decorator adds to the class is injected from the
surrounding scope.
"""
def render_templex(self):
templex_template = django_template.loader.get_template(self.template)
data_dict = dataclasses.asdict(self)
return templex_template.render(data_dict)

@functools.wraps(klass)
def wrap(klass: Type):
klass = dataclasses.dataclass(klass)
if not hasattr(klass, "template"):
klass.template = template
if not hasattr(klass, "render"):
klass.render = render_templex
return klass

return wrap(klass)

return wrapper


@templex(template="molecules/teaser/teaser.html")
class Teaser:
heading: str
introduction: str
href: str
Expand Down
5 changes: 2 additions & 3 deletions lpld/templates/pages/home/home.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{% extends "base-page.html" %}
{% load templex %}
{% load slippers static templex wagtailcore_tags wagtailimages_tags %}
{% load slippers static wagtailcore_tags wagtailimages_tags %}

{% block content %}
<section class="mt-44">
Expand Down Expand Up @@ -47,7 +46,7 @@
<ul class="mt-16 grid grid-cols-1 md:grid-cols-2 md:gap-x-12 gap-y-12 sm:gap-y-16 ">
{% for teaser in page.project_teasers %}
<li>
{% include_templex teaser %}
{{ teaser.render }}
</li>
{% endfor %}
</ul>
Expand Down
11 changes: 0 additions & 11 deletions lpld/templatetags/templex.py

This file was deleted.

0 comments on commit 9c7c84c

Please sign in to comment.