Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 486
MikeN Avatar asked Oct 15 '25 14:10

MikeN


1 Answers

from django.template import Context, Template

template = Template("this is a template string! {{ foo }}")
c = Context({"foo": "barbarbar"})
print template.render(c)
like image 198
Mathiasdm Avatar answered Oct 17 '25 08:10

Mathiasdm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!