Reputation: 6430
Is there any way to give the template multiple variables through bottle? i know that you can use template('mytemplate.tpl',var=var)
and such, however, how can you use multiple variables?
Upvotes: 0
Views: 3849
Reputation: 1
values = {'name':name, 'gender':gender, 'age':age, 'address':address}
template('mytemplate', var = values)
In your template, you access the variables as var.name
, var.gender
, var.age
, var.address
Upvotes: 0
Reputation: 865
The method which I prefer is this:
@route('/')
@view('mytemplate') #no .tpl
def mypage():
return {"name": "Anne", "address": "4 Elm Street", "dob": datetime.datetime(1977,12,2,1,2,3)}
This makes handling multiple return statements much easier (although some people consider multiple return statements bad practice), and I find it is much easier to change the template file, as I don't need to go find it every time.
Or if you prefer the a=b, b=c, etc. method
return dict(name="Anne", address="4 Elm Street", dob=datetime.datetime(1977,12,2,1,2,3))
Upvotes: 0
Reputation: 4367
As you can see in the signature, one can pass any number of variables to the template by using keyword arguments:
template('mytemplate.tpl', name="Anne", address="4 Elm Street",
dob=datetime.datetime(1977,12,2,1,2,3))
or like this
d = { "name": "Anne", "address": "4 Elm Street", "dob": datetime.datetime(1977,12,2,1,2,3) }
template('mytemplate.tpl', **d)
Upvotes: 4