Question

Rendering a view using template():

@get('/start/<page:int>')
def start(page=1):
    return template('start', page=page)

Same example using a view() decorator:

@get('/start/<page:int>')
@view('start')
def start(page=1):
    return dict(page=page)

Is there any difference between the two other than personal preference?

Was it helpful?

Solution

Well, I am not a professional developer so I may say something that goes against most basic good manners in coding, but I find more useful to use return template() because I can use several templates, while with view decorator that is not possible. E.G:

@get('/start/<page:int>')
def start(page=1):
    return template('header',username=username)+template('start', page=page)+template('foot')

Of course it is possible to do this with @views and %include subtemplates in the template, and probably in one million other ways too, but... it's a difference!

...Or is it possible to call several templates within one @view?

OTHER TIPS

My understanding is: no, there is no difference other than preference. (The same way you could write a plugin or use a route decorator to accomplish many of the same tasks.) They are equivalent ways of accomplishing the same goal: rendering a template.

The Bottle template docs support this:

To render a template you can use the template() function or the view() decorator.

...

The view() decorator allows you to return a dictionary with the template variables instead of calling template().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top