diff --git a/taccsite_cms/settings.py b/taccsite_cms/settings.py index f8ebfebee..2bc899eda 100644 --- a/taccsite_cms/settings.py +++ b/taccsite_cms/settings.py @@ -226,10 +226,9 @@ def gettext(s): return s 'django_settings_export.settings_export' ], 'libraries': { - # NOTE: These are an unnecessary alternative config, because taccsite_cms is in INSTALLED_APPS, but are comfortably explicit - # SEE: https://docs.djangoproject.com/en/3.1/howto/custom-template-tags/#code-layout 'custom_portal_settings': 'taccsite_cms.templatetags.custom_portal_settings', 'tacc_uri_shortcuts': 'taccsite_cms.templatetags.tacc_uri_shortcuts', + 'get_url_match': 'taccsite_cms.templatetags.get_url_match', }, 'loaders': [ 'django.template.loaders.filesystem.Loader', 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..acafb8c43 160000 --- a/taccsite_custom +++ b/taccsite_custom @@ -1 +1 @@ -Subproject commit a87ede7e6b064712c5ee9cb4d8c380343ecf26f0 +Subproject commit acafb8c433c6dd20803d794847c62b7b779b5fba