I have a template which displays a lot of values which are passed from a server, my question is how to i pass these values to the template file. My Handler code is as follows: class AdminHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): #respond to a get method #self.write("AdminHandler:: Inside GET function")
userName = "Alwin Doss"
welcomeMessage = "Good evening are you enjoying kids dance"
items = {}
items["userName"] = userName
items["welcomeMessage"] = welcomeMessage
self.render("web/admin.html", title="Admin Page", items=items)
and my template code is as follows: {% items['userName'] %} {% items['welcomeMessage'] %} {% end %}
Problem is that I am unable to access these values in the template file. I get the following error:
raise ParseError("unknown operator: %r" % operator) ParseError: unknown operator: "items['userName']" ERROR:root:500 GET /admin (127.0.0.1) 3.27ms
Here is a demonstration similar to what you seem to be doing. Look into the syntax of the template and see the different uses of {% %}
and the {{ }}
blocks. This code:
from tornado import template
t = template.Template('''\
{% for user in users %}
{{ user['userName'] }}
{{ user['welcomeMessage'] }}
{% end %}
''')
# create first user and append to a user list
users = []
user = { "userName" : "Alwin Doss",
"welcomeMessage" : "Good evening are you enjoying kids dance"}
users.append(user)
# create and append second user
user = { "userName" : "John Smith",
"welcomeMessage" : "Good evening, JS"}
users.append(user)
# render the template and output to console
print t.generate(users = users)
Produces this output:
Alwin Doss
Good evening are you enjoying kids dance
John Smith
Good evening, JS
For more on Tornado templates have a look at this tutorial and of course at the Tornado templates documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With