Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added custom templatetag get_latest_blog_entry for django_website
git-svn-id: http://code.djangoproject.com/svn/djangoproject.com@33 bcc190cf-cafb-0310-a4f2-bffc1f526a37
- Loading branch information
adrian
committed
Jul 14, 2005
1 parent
1fe4d38
commit d7b7730
Showing
3 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,27 @@ | |||
from django.core import template | |||
from django.models.blog import entries | |||
|
|||
class LatestBlogEntryNode(template.Node): | |||
def __init__(self, varname): | |||
self.varname = varname | |||
|
|||
def render(self, context): | |||
try: | |||
e = entries.get_latest() | |||
except entries.EntryDoesNotExist: | |||
e = None | |||
context[self.varname] = e | |||
return '' | |||
|
|||
def do_get_latest_blog_entry(parser, token): | |||
""" | |||
{% get_latest_blog_entry as latest_entry %} | |||
""" | |||
bits = token.contents.split() | |||
if len(bits) != 3: | |||
raise template.TemplateSyntaxError, "'%s' tag takes two arguments" % bits[0] | |||
if bits[1] != 'as': | |||
raise template.TemplateSyntaxError, "First argument to '%s' tag must be 'as'" % bits[0] | |||
return LatestBlogEntryNode(bits[2]) | |||
|
|||
template.register_tag('get_latest_blog_entry', do_get_latest_blog_entry) |