Skip to content

Commit

Permalink
♻️ [#2062] Generic create_content_wrapper to wrap content in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Feb 20, 2024
1 parent 02f9b4c commit 5444701
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 29 deletions.
10 changes: 4 additions & 6 deletions src/open_inwoner/components/templatetags/button_tags.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django import template
from django.urls import NoReverseMatch, reverse

from open_inwoner.components.utils import ContentsNode, parse_component_with_args
from .helpers import create_content_wrapper

register = template.Library()

Expand All @@ -25,11 +25,9 @@ def button_row(parser, token):
Extra context:
- contents: string (HTML) | this is the context between the button_row and endbutton_row tags
"""
bits = token.split_contents()
context_kwargs = parse_component_with_args(parser, bits, "button_row")
nodelist = parser.parse(("endbutton_row",))
parser.delete_first_token()
return ContentsNode(nodelist, "components/Button/ButtonRow.html", **context_kwargs)
return create_content_wrapper("button_row", "components/Button/ButtonRow.html")(
parser, token
)


@register.inclusion_tag("components/Button/Button.html")
Expand Down
10 changes: 4 additions & 6 deletions src/open_inwoner/components/templatetags/card_tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django import template

from open_inwoner.components.utils import ContentsNode, parse_component_with_args
from .helpers import create_content_wrapper

register = template.Library()

Expand All @@ -21,8 +21,6 @@ def render_card(parser, token):
Extra context:
- contents: string (HTML) | this is the context between the render_card and endrender_card tags
"""
bits = token.split_contents()
context_kwargs = parse_component_with_args(parser, bits, "render_card")
nodelist = parser.parse(("endrender_card",))
parser.delete_first_token()
return ContentsNode(nodelist, "components/Card/RenderCard.html", **context_kwargs)
return create_content_wrapper("render_card", "components/Card/RenderCard.html")(
parser, token
)
10 changes: 4 additions & 6 deletions src/open_inwoner/components/templatetags/dropdown_tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django import template

from open_inwoner.components.utils import ContentsNode, parse_component_with_args
from .helpers import create_content_wrapper

register = template.Library()

Expand Down Expand Up @@ -30,8 +30,6 @@ def dropdown(parser, token):
Extra context:
- contents: string (HTML) | this is the context between the dropdown and enddropdown tags
"""
bits = token.split_contents()
context_kwargs = parse_component_with_args(parser, bits, "dropdown")
nodelist = parser.parse(("enddropdown",))
parser.delete_first_token()
return ContentsNode(nodelist, "components/Dropdown/Dropdown.html", **context_kwargs)
return create_content_wrapper("dropdown", "components/Dropdown/Dropdown.html")(
parser, token
)
10 changes: 5 additions & 5 deletions src/open_inwoner/components/templatetags/grid_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from open_inwoner.components.utils import ContentsNode, parse_component_with_args

from .helpers import create_content_wrapper

register = template.Library()


Expand All @@ -22,11 +24,9 @@ def render_grid(parser, token):
Extra context:
- contents: string (HTML) | this is the context between the render_grid and endrender_grid tags
"""
bits = token.split_contents()
context_kwargs = parse_component_with_args(parser, bits, "render_grid")
nodelist = parser.parse(("endrender_grid",))
parser.delete_first_token()
return ContentsNode(nodelist, "components/Grid/Grid.html", **context_kwargs)
return create_content_wrapper("render_grid", "components/Grid/Grid.html")(
parser, token
)


@register.tag
Expand Down
13 changes: 13 additions & 0 deletions src/open_inwoner/components/templatetags/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.urls import reverse
from django.utils.html import format_html_join

from open_inwoner.components.utils import ContentsNode, parse_component_with_args

register = template.Library()


Expand Down Expand Up @@ -67,3 +69,14 @@ def as_attributes(attribute_dict):
return format_html_join(" ", '{}="{}"', attribute_dict.items())
except AttributeError:
return attribute_dict


def create_content_wrapper(name, template_name):
def inner(parser, token):
bits = token.split_contents()
context_kwargs = parse_component_with_args(parser, bits, name)
nodelist = parser.parse((f"end{name}",))
parser.delete_first_token()
return ContentsNode(nodelist, template_name, **context_kwargs)

return inner
10 changes: 4 additions & 6 deletions src/open_inwoner/components/templatetags/list_tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django import template

from open_inwoner.components.utils import ContentsNode, parse_component_with_args
from .helpers import create_content_wrapper

register = template.Library()

Expand All @@ -18,11 +18,9 @@ def render_list(parser, token):
Extra context:
- contents: string (HTML) | this is the context between the render_list and endrender_list tags
"""
bits = token.split_contents()
context_kwargs = parse_component_with_args(parser, bits, "render_list")
nodelist = parser.parse(("endrender_list",))
parser.delete_first_token()
return ContentsNode(nodelist, "components/List/List.html", **context_kwargs)
return create_content_wrapper("render_list", "components/List/List.html")(
parser, token
)


@register.inclusion_tag("components/List/ListItem.html")
Expand Down

0 comments on commit 5444701

Please sign in to comment.