Python的Flask框架标配模板引擎Jinja2的使用教程
Jinja2需要Python2.4以上的版本。 #sudo easy_install Jinja2 #sudo pip install Jinja2 这两个工具可以自动从网站上下载Jinja,并安装到python目录的site-packages目录中。
从tar包安装: # 下载Jinja的安装包 # 解压缩 # sudo python setup.py install 基本API用法 <pre> >>> from Jinja2 import Template >>> template = Template('Hello {{ name }}!') >>> template.render(name='World') u'Hello World!' </pre> 这个例子使用字符串作为模板内容创建了一个Template实例,然后用"name='World'"作为参数调用"render方法,将内容中 的'name'替换为"World",最终返回渲染过的字符串--"u'Hello World!'"。 如何组织模板 myapp/ __init__.py models.py views/ templates/ static/ run.py requirements.txt templates/ layout.html index.html about.html profile/ layout.html index.html photos.html admin/ layout.html index.html analytics.html templates 目录的结构是与我们路由结构平行的。对于路由 myapp.com/admin/analytics 的模板就是 templates/admin/analytics.html。在目录里面还有一些额外的模板,它们不会直接地被渲染。layout.html 文件是为了让其它的模板继承。 继承 {# _myapp/templates/layout.html_ #} <!DOCTYPE html> <html lang="en"> <head> <title>{% raw %}{% block title %}{% endblock %}{% endraw %}</title> </head> <body> {% block body %} <h1>This heading is defined in the parent.</h1> {% endblock %} </body> </html> 在子模板中,我们可以扩展父模板并且定义这些块的内容。 {# _myapp/templates/index.html_ #} {% extends "layout.html" %} {% block title %}Hello world!{% endblock %} {% block body %} {{ super() }} <h2>This heading is defined in the child.</h2> {% endblock %} super() 函数让我们渲染父级块的内容。 创建宏 {# myapp/templates/layout.html #} {% from "macros.html" import nav_link with context %} <!DOCTYPE html> <html lang="en"> <head> {% block head %} <title>My application</title> {% endblock %} </head> <body> <ul class="nav-list"> {{ nav_link('home','Home') }} {{ nav_link('about','About') }} {{ nav_link('contact','Get in touch') }} </ul> {% block body %} {% endblock %} </body> </html> 在这个模板中我们现在要做的就是调用一个未定义的宏 - nav_link -接着向其传递两个参数:目标端点(例如,目标视图的函数名)以及我们要显示的文本。 {# myapp/templates/macros.html #} {% macro nav_link(endpoint,text) %} {% if request.endpoint.endswith(endpoint) %} <li class="active"><a href="{{ url_for(endpoint) }}">{{text}}</a></li> {% else %} <li><a href="{{ url_for(endpoint) }}">{{text}}</a></li> {% endif %} {% endmacro %} 现在我们已经在 myapp/templates/macros.html 中定义了宏。在这个宏中我们使用了 Flask 的 request 对象 ― 默认情况下在 Jinja 上下文中是可用的 ― 用来检查传入到 nav_link 中的路由的端点是否是当前请求。如果是,我们正在当前页面上,接着我们标记它为活跃的。 自定义过滤器 <h2>{{ article.title|title }}</h2> 在这段代码中,title 过滤器接收 article.title 作为参数并且返回一个过滤后的标题,接着过滤后的标题将会输出到模板中。这就像 UNIX 的“管道化”一个程序到另一个程序的输出。 # myapp/util/filters.py from .. import app @app.template_filter() def caps(text): """Convert a string to all caps.""" return text.uppercase() 在这段代码中我们使用 @app.template_filter() 装饰器注册我们的函数成一个 Jinja 过滤器。默认的过滤器名称就是函数的名称,但是你可以传入一个参数到装饰器中来改变它。 @app.template_filter('make_caps') def caps(text): """Convert a string to all caps.""" return text.uppercase() 现在我们可以在模板中调用 make_caps 而不是 {% raw %}caps:{{ "hello world!"|make_caps }}{% endraw %}。 # myapp/__init__.py # Make sure app has been initialized first to prevent circular imports. from .util import filters (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |