2010-02-23 88 views
0

我使用App Engine的web應用程序的模板系統(類似,如果不完全相同的Django)Python的模板幫助

通常我渲染模板,從我的靜態目錄/模板/作爲 跟隨我的主要的處理程序:

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) 

對於我目前的應用程序,我想渲染一個包含在 字符串中的模板。我使用下面的代碼:

t = template.Template(template_string) 
c = template.Context(template_values) 
output = t.render(c) 
self.response.out.write(output) 

它呈現模板,但不是「包括」包含在 的字符串變量。例如,字符串模板

的 「hello world {%包括 '的helloworld.html' %}」

呈現的 「Hello World」,但不會呈現 '的helloworld.html' 的內容。 我猜這與'helloworld.html'相同的目錄中的字符串不是 ,但我不知道如何 指定include標籤應該在'/ templates/* 「 任何幫助,將不勝感激, 阿瓊

回答

0
{% include 'dirname/helloworld.html' %} 

應該努力!

3

webapp框架使用Django 0.9.6模板。如果您正如上面所描述的那樣從字符串中加載模板,則需要configure the template loader,以便它可以找到從文件加載的依賴關係。 Here's webapp如何配置它們。