Commits

Steve Losh  committed f709e8c

Add the templatetags.

  • Participants
  • Parent commits c01f28f

Comments (0)

Files changed (3)

File requirements.txt

 django-treebeard
+django-templatetag-sugar
+

File stoat/templatetags/__init__.py

Empty file added.

File stoat/templatetags/stoat.py

+from django import template
+from django.shortcuts import get_object_or_404
+from django.http import Http404
+from templatetag_sugar.parser import Name, Constant, Optional
+from templatetag_sugar.register import tag
+
+from .. import models
+
+register = template.Library()
+
+@tag(register, [Optional([Constant("as"), Name()])])
+def current_page(context, asvar=None):
+    url = context['request'].path_info.rstrip('/') + '/'
+    try:
+        page = get_object_or_404(models.Page, url__exact=url)
+    except Http404:
+        page = None
+
+    if asvar:
+        context[asvar] = page
+    else:
+        context['page'] = page
+
+    return ''
+