简体   繁体   English

瓶@view vs模板()

[英]Bottle @view vs template()

Rendering a view using template() : 使用template()渲染视图:

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

Same example using a view() decorator: 使用view()装饰器的相同示例:

@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? 除了个人偏好之外,两者之间是否有任何区别?

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. 好吧,我不是一个专业的开发人员,所以我可能会说一些与编码中最基本的好方法相悖的东西,但我觉得使用return template()会更有用,因为我可以使用多个模板,而使用不可能的视图装饰器。 EG: 例如:

@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! 当然可以在模板中使用@views和%include子模板来实现这一点,也可能在其他一百万种方式中实现这一点,但是......这是一个区别!

...Or is it possible to call several templates within one @view? ...或者是否可以在一个@view中调用多个模板?

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: Bottle 模板文档支持这一点:

To render a template you can use the template() function or the view() decorator. 要渲染模板,您可以使用template()函数或view()装饰器。

... ...

The view() decorator allows you to return a dictionary with the template variables instead of calling template(). view()装饰器允许您返回带有模板变量的字典,而不是调用template()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM