Re-using Template Snippets - Where's my {%with%}?

3 views
Skip to first unread message

Nick Retallack

unread,
Jul 27, 2008, 7:12:48 PM7/27/08
to Google App Engine
In Django, I was able to re-use template snippets like this:

{% with your_things as those_things %}
{% include 'things_gallery.html' %} {# displays those_things #}
{% endwith %}

{% with my_things as those_things %}
{% include 'things_gallery.html' %}
{% endwith %}

However, I since I switched to appengine and implemented my app in
webapp, I get this when I try to use those templates:
TemplateSyntaxError: Invalid block tag: 'with'

Can I add 'with' in somehow? Is there some other way to re-use
snippets?
(Hm. I suppose I could render the snippets first, and then pass them
in as strings to their parent?)

I'd happily switch to a different template language that would allow
me to re-use snippets, but it looks like most template languages don't
work on appengine. There are postings on this board about how mako
and genshi don't work. What about tenjin? I haven't got an appengine
account yet -- can someone test tenjin out for me?

Roberto Saccon

unread,
Jul 27, 2008, 8:41:44 PM7/27/08
to Google App Engine
"with" is only in django > 0.96, but appengine uses django 0.96

The recommended way (watch the slides/videos form the google IO event)
is using the appengine django helper and django trunk, then you have
the most similar setup compared to original django

regards
Roberto

Nick Retallack

unread,
Jul 27, 2008, 9:59:38 PM7/27/08
to Google App Engine
Well, I don't really need django for anything else, so I'd rather not
do too much complex setup to import a new version of it. If that was
what I'd have to do, I'd go with my little hack solution of rendering
snippets first and then putting them into the main template.

However, I am curious about other templating engines that people have
gotten appengine to support! I mean, it claims to run them, but I've
seen plenty of topics here saying they don't work.

Jimmie Tyrrell

unread,
Aug 29, 2008, 1:45:57 AM8/29/08
to Google App Engine
Replacement for the time being. Place it in a separate .py file:

import re, string
from django.template import Node
from google.appengine.ext import webapp

register = webapp.template.create_template_register()

class WithNode(Node):
def __init__(self, var, name, nodelist):
self.var = var
self.name = name
self.nodelist = nodelist

def render(self, context):
val = self.var.resolve(context)
context.push()
context[self.name] = val
output = self.nodelist.render(context)
context.pop()
return output

#@register.tag
def do_with(parser, token):
bits = list(token.split_contents())
if len(bits) != 4 or bits[2] != "as":
raise TemplateSyntaxError, "%r expected format is 'value as
name'" % tagname
var = parser.compile_filter(bits[1])
name = bits[3]
nodelist = parser.parse(('endwith',))
parser.delete_first_token()
return WithNode(var, name, nodelist)

do_with = register.tag('with', do_with)

# Taken from django source code
# Just load it up in your template

Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages