diff --git a/taccsite_cms/templatetags/get_url_match.py b/taccsite_cms/templatetags/get_url_match.py new file mode 100644 index 000000000..6bb22c0ec --- /dev/null +++ b/taccsite_cms/templatetags/get_url_match.py @@ -0,0 +1,43 @@ +import re + +from django import template +from urllib.parse import urlparse + +register = template.Library() + +@register.filter +def get_url_match(path, pattern): + """ + Custom Template Filter `get_url_match` + Use: Render string that matches given pattern from current page URL. + Load custom filter into template: + {% load get_url_match %} + Template inline usage: + {# given path '.../2019/...', does nothing #} + {% if path|get_url_match:"/20\d\d/" %} + {# condition evaluates to False #} + {% endif %} + {# given path '.../2020/...', does something #} + {% if path|get_url_match:"/20\d\d/" %} + {# condition evaluates to True #} + {% endif %} + {# given path '.../2021/...', prints complete match #} + {% with year_path=path|get_url_match:"/20\d\d/" %} +
{{ year_path }}
{# prints complete match #} + {% endwith %} + {# given path '.../2022/...', prints matched content #} + {% with year_slug=path|get_url_match:"/(20\d\d)/" %} +
{{ year_slug }}
{# prints match within the (…) #} + {% endwith %} + """ + result = re.search(pattern, path) + + if result: + try: + match = result.group(1) + except IndexError: + match = result[0] + else: + match = False + + return match diff --git a/taccsite_custom b/taccsite_custom index a87ede7e6..32c34856f 160000 --- a/taccsite_custom +++ b/taccsite_custom @@ -1 +1 @@ -Subproject commit a87ede7e6b064712c5ee9cb4d8c380343ecf26f0 +Subproject commit 32c34856f844fe30b3009f9a10deae2d384c078f