Use of Globals in Template as String

24 views
Skip to first unread message

Shane Cleveland

unread,
Mar 14, 2017, 11:45:17 PM3/14/17
to web.py
I am needing to use a template as a string, so I can send it as an email from within my application.

The web.py docs show how to do this:

template = "$def with (name)\nHello $name"
hello = web.template.Template(template)
print hello('world')

But I need to use the round() function, which is not supported within the template. I was able to successfully pass this as a global when rendering the html in a browser, but I have been able to figure out how to do it with the template as a string.

Any ideas?

Thanks.

Anand Chitipothu

unread,
Mar 14, 2017, 11:53:16 PM3/14/17
to webpy
Here is what you need to do:

hello = web.template.Template(template, globals={"round": round})

Anand

Shane Cleveland

unread,
Mar 15, 2017, 12:28:31 PM3/15/17
to web.py
Thanks, Anand. That did it.

Shane
Reply all
Reply to author
Forward
0 new messages