在内存中调用Django模板渲染器,而不需要字符串中的任何文件? [英] Invoke Django template renderer in memory without any files from strings?

查看:107
本文介绍了在内存中调用Django模板渲染器,而不需要字符串中的任何文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为基于Django模板语言的用户构建了一种宏语言。用户输入UITextFields可以在较大文档的上下文中呈现的模板/宏片段。所以我有很多django模板代码的多行字符串片段,应该使用也存储在内存中的变量来填充。我不想将任何东西转储到文件中,我需要渲染这些模板

I have built a Macro language for my users that is based upon the Django template language. Users enter into UITextFields their template/macro snippets that can be rendered in the context of larger documents. So I have large multi-line string snippets of django template code that should be populated with variables that are also stored in memory. I don't want to ever have to dump anything to files, I need to render these template

如何在存储的模板中调用Django模板渲染器内存中的字符串(在python实例变量中)?应该填充该模板的变量也是存储在内存中的实例变量。

How can I invoke the Django template renderer on a template that is stored in a string in memory (in python instance variables)? The variables that should populate that template are also instance variables stored in memory.

推荐答案

from django.template import Context, Template

template = Template("this is a template string! {{ foo }}")
c = Context({"foo": "barbarbar"})
print template.render(c)

这篇关于在内存中调用Django模板渲染器,而不需要字符串中的任何文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆