I'm using App Engine's web-app templating s开发者_StackOverflow社区ystem (similar if not identical to django)
Normally I render templates from my static directory /templates/ as follows in my main handler:
dirname = os.path.dirname(__file__)
template_file = os.path.join(dirname, os.path.join('templates', template_name))
output = template.render(template_file,template_values)
self.response.out.write(output)
For my current app, I want to render a template contained in a string. I'm using the following code:
t = template.Template(template_string)
c = template.Context(template_values)
output = t.render(c)
self.response.out.write(output)
It renders the template, but not the "include" tags contained within the string. For example, the string template
"hello world {% include 'helloworld.html' %}"
renders "hello world" but does not render the contents of 'helloworld.html'. I'm guessing that this has something to do with the string not being in the same directory as 'helloworld.html', but I'm not sure how to specify that the include tags should look in '/templates/*' Any help would be appreciated, Arjun
The webapp framework uses Django 0.9.6 templates. If you're loading templates from a string as you describe above, you need to configure the template loader so it can find dependencies loaded from files. Here's how webapp configures them.
{% include 'dirname/helloworld.html' %}
should work!
精彩评论