像MVC 3烧瓶局部视图 [英] Flask partial view like MVC 3

查看:137
本文介绍了像MVC 3烧瓶局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有像.net MVC 3的部分视图在烧瓶?


我想在一个视图页面中嵌入一个小部件,并且这个小部件有它自己的逻辑。

解决方案<



/ templates / #include> include语句将呈现提供的视图(默认情况下为当前上下文):

  {#在your_view_template.jinja#} 
{#...您的代码...#}
{%includewidgets / your_widget。 jinja%}
{#...您的代码...#}

您也可以定义 import 到视图模板中:

  {#your_view_template.jinja#} 
{%importwidgets / your_widget.jinjaas your_widget%}
{#...您的代码...#}
{{you_widget.render(您的,重要的,变量等) )}}
{# ...你的代码...



import include 可以使用变量,所以类似这样的情况是可能的:

 #在您的视图
中,如果complex_conditions.are_true():
widget =widgets / special_custom_widget.jinja
else:
widget =widgets / boring_widget.jinja
render_template(your_view.jinja,widget = widget)

{#your_view_template.jinja#}
{%include widget%}
{#
导入小部件作为sidebar_widget
{{sidebar_widget.render()}}
也可以工作
#}

这些都与MVC的部分视图类似(至少,据我了解)

另外,如果你的widget需要访问ACL或模板层不可用的信息,并且无法重新编写视图以利用 include import 你可以拿@ Alex Morega的建议在你的视图中
render_template() your_view.jinja,widget = you_callable等等等等)

{#your_view_template.jinja#}
{#...你的代码...#}
{{widget()}}
{#或者,如果您返回的不是标记构造的HTML,则返回$ b $ {{widget()|安全}}
{#...您的代码...#}

甚至创建您自己的模板加载器并加载不同的模板,具体取决于任何。但是,这肯定是矫枉过正的这种情况。


Is there something like .net MVC 3's partial view in flask?
I want to embed a widget in one view page,and that widget has its own logic.

解决方案

There are several ways to include content in a Jinja2 template:

The include statement will render the supplied view (with the current context by default):

{# In your_view_template.jinja #}
{# ... your code ... #}
{% include "widgets/your_widget.jinja" %}
{# ... your code ... #}

You can also define macros and import them into the view template:

{# In your_view_template.jinja #}
{% import "widgets/your_widget.jinja" as your_widget %}
{# ... your code ... #}
{{ you_widget.render(your, important, variables, etc.) }}
{# ... your code ... #}

Both import and include can use variables, so something like this is possible:

# In your view
if complex_conditions.are_true():
    widget = "widgets/special_custom_widget.jinja"
else:
    widget = "widgets/boring_widget.jinja"
render_template("your_view.jinja", widget=widget)

{# In your_view_template.jinja #}
{% include widget %}
{# 
import widget as sidebar_widget 
{{ sidebar_widget.render() }}
would also work 
#}

These both work similarly to MVC's partial views (at least, inasmuch as I understand them)

Alternately, if your widget needs access to ACLs or information that should not be available to the template layer and you cannot re-write your view to take advantage of include and import you can take @[Alex Morega]'s suggestion and pass in a callable as a variable to the template and render it there.

# In your view
render_template("your_view.jinja", widget=you_callable, etc, etc, etc)

{# In your_view_template.jinja #}
{# ... your code ... #}
{{ widget() }}
{# Or, if you are returning HTML that is not a Markup construct #}
{{ widget() | safe }}
{# ... your code ... #}

You could even create your own template loader and load different templates depending on almost anything. But that would definitely be overkill for this case.

这篇关于像MVC 3烧瓶局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆