Invoke Django template renderer in memory without any files from strings?

僤鯓⒐⒋嵵緔 提交于 2019-12-06 06:29:59

问题


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

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.


回答1:


from django.template import Context, Template

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


来源:https://stackoverflow.com/questions/3077272/invoke-django-template-renderer-in-memory-without-any-files-from-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!