python - Flask partial view like MVC 3 -
is there .net mvc 3's partial view in flask?
want embed widget in 1 view page,and widget has own logic.
there several ways include content in jinja2 template:
the include
statement render supplied view (with current context default):
{# in your_view_template.jinja #} {# ... code ... #} {% include "widgets/your_widget.jinja" %} {# ... code ... #}
you can define macros , import them view template:
{# in your_view_template.jinja #} {% import "widgets/your_widget.jinja" your_widget %} {# ... code ... #} {{ you_widget.render(your, important, variables, etc.) }} {# ... code ... #}
both import
, include
can use variables, possible:
# in 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 sidebar_widget {{ sidebar_widget.render() }} work #}
these both work mvc's partial views (at least, inasmuch understand them)
alternately, if widget needs access acls or information should not available template layer , cannot re-write view take advantage of include
, import
can take @[alex morega]'s suggestion , pass in callable variable template , render there.
# in view render_template("your_view.jinja", widget=you_callable, etc, etc, etc) {# in your_view_template.jinja #} {# ... code ... #} {{ widget() }} {# or, if returning html not markup construct #} {{ widget() | safe }} {# ... code ... #}
you even create own template loader , load different templates depending on anything. overkill case.
Comments
Post a Comment