Skip to content

Commit

Permalink
1. get_page template tag can now accept context variable for slug or id
Browse files Browse the repository at this point in the history
2. IMPORTANT API CHANGE: get_page now required quotes when a slug string is directly used, as stated in the documentation
  • Loading branch information
Batiste Bieler committed Mar 15, 2011
1 parent 27c1874 commit 5eb6fe3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 6 additions & 5 deletions pages/templatetags/pages_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,13 @@ def do_csrf_token(parser, token):

class GetPageNode(template.Node):
"""get_page Node"""
def __init__(self, page, varname):
self.page = page
def __init__(self, page_filter, varname):
self.page_filter = page_filter
self.varname = varname

def render(self, context):
page = get_page_from_string_or_id(self.page)
page_or_id = self.page_filter.resolve(context)
page = get_page_from_string_or_id(page_or_id)
context[self.varname] = page
return ''

Expand All @@ -320,9 +321,9 @@ def do_get_page(parser, token):
if bits[-2] != 'as':
raise TemplateSyntaxError(
'%r expects "as" as the second argument' % bits[0])
page = bits[1]
page_filter = parser.compile_filter(bits[1])
varname = bits[-1]
return GetPageNode(page, varname)
return GetPageNode(page_filter, varname)
do_get_page = register.tag('get_page', do_get_page)


Expand Down
6 changes: 3 additions & 3 deletions pages/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,16 @@ def test_placeholder_inherit_content(self):
def test_get_page_template_tag(self):
"""Test get_page template tag."""
context = Context({})
pl1 = """{% load pages_tags %}{% get_page get-page-slug as toto %}{{ toto }}"""
pl1 = """{% load pages_tags %}{% get_page "get-page-slug" as toto %}{{ toto }}"""
template = get_template_from_string(pl1)
self.assertEqual(template.render(context), u'None')
page = self.new_page({'slug':'get-page-slug'})
page = self.new_page({'slug': 'get-page-slug'})
self.assertEqual(template.render(context), u'get-page-slug')

def test_placeholder_all_syntaxes(self):
"""Test placeholder syntaxes."""
page = self.new_page()
context = Context({'current_page': page, 'lang':'en-us'})
context = Context({'current_page': page, 'lang': 'en-us'})

pl1 = """{% load pages_tags %}{% placeholder title as hello %}"""
template = get_template_from_string(pl1)
Expand Down

0 comments on commit 5eb6fe3

Please sign in to comment.