美文网首页
2.1 模板简介

2.1 模板简介

作者: yungege | 来源:发表于2019-08-19 11:38 被阅读0次

模板简介:

在之前的章节中,视图函数只是直接返回文本,而在实际生产环境中其实很少这样用,因为实际的页面大多是带有样式和复杂逻辑的HTML代码,这可以让浏览器渲染出非常漂亮的页面。目前市面上有非常多的模板系统,其中最知名最好用的就是Jinja2Mako,我们先来看一下这两个模板的特点和不同:

  1. Jinja2:Jinja是日本寺庙的意思,并且寺庙的英文是temple和模板的英文template的发音类似。Jinja2是默认的仿Django模板的一个模板引擎,由Flask的作者开发。它速度快,被广泛使用,并且提供了可选的沙箱模板来保证执行环境的安全,它有以下优点:

    • 让前端开发者和后端开发者工作分离。

    • 减少Flask项目代码的耦合性,页面逻辑放在模板中,业务逻辑放在视图函数中,将页面逻辑和业务逻辑解耦有利于代码的维护。

    • 提供了控制语句、继承等高级功能,减少开发的复杂度。

  2. Marko:Marko是另一个知名的模板。他从DjangoJinja2等模板借鉴了很多语法和API,他有以下优点:

    • 性能和Jinja2相近,在这里可以看到。

    • 有大型网站在使用,有成功的案例。Reddit和豆瓣都在使用。

    • 有知名的web框架支持。PylonsPyramid这两个web框架内置模板就是Mako

    • 支持在模板中写几乎原生的Python语法的代码,对Python工程师比较友好,开发效率高。

    • 自带完整的缓存系统。当然也提供了非常好的扩展借口,很容易切换成其他的缓存系统。

Flask渲染Jinja模板:

要渲染一个模板,通过render_template方法即可,以下将用一个简单的例子进行讲解:

<pre class="md-fences md-end-block md-fences-with-lineno" lang="python" contenteditable="false" cid="n40" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; font-size: 0.9rem; white-space: pre; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); text-align: left; break-inside: avoid; display: block; position: relative !important; padding: 10px 30px 10px 0px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> from flask import Flask,render_template
app = Flask(name)

@app.route('/about/')
def about():
return render_template('about.html')</pre>

当访问/about/的时候,about()函数会在当前目录下的templates文件夹下寻找about.html模板文件。如果想更改模板文件地址,应该在创建app的时候,给Flask传递一个关键字参数template_folder,指定具体的路径,再看以下例子:

<pre class="md-fences md-end-block md-fences-with-lineno" lang="python" contenteditable="false" cid="n43" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; font-size: 0.9rem; white-space: pre; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); text-align: left; break-inside: avoid; display: block; position: relative !important; padding: 10px 30px 10px 0px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
from flask import Flask,render_template
app = Flask(name,template_folder=r'C:\templates')

@app.route('/about/')
def about():
return render_template('about.html')</pre>

以上例子将会在C盘的templates文件夹中寻找模板文件。还有最后一点是,如果模板文件中有参数需要传递,应该怎么传呢,我们再来看一个例子:

<pre class="md-fences md-end-block md-fences-with-lineno" lang="python" contenteditable="false" cid="n46" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; font-size: 0.9rem; white-space: pre; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); text-align: left; break-inside: avoid; display: block; position: relative !important; padding: 10px 30px 10px 0px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
from flask import Flask,render_template
app = Flask(name)

@app.route('/about/')
def about():
# return render_template('about.html',user='xiaotuo')
return render_template('about.html',**{'user':'xiaotuo})</pre>

以上例子介绍了两种传递参数的方式,因为render_template需要传递的是一个关键字参数,所以第一种方式是顺其自然的。但是当你的模板中要传递的参数过多的时候,把所有参数放在一个函数中显然不是一个好的选择,因此我们使用字典进行包装,并且加两个*号,来转换成关键字参数。

相关文章

  • 2.1 模板简介

    模板简介: 在之前的章节中,视图函数只是直接返回文本,而在实际生产环境中其实很少这样用,因为实际的页面大多是带有样...

  • 2.1 简介

    php支持8种原始数据类型 四种标量类型 boolean 布尔类型 integer 整型 float 浮点型(也称...

  • 2.1. 简介

    简介2.3. 创建扩展包 |《Composer 中文文档》| PHP 技术论坛 Composer 是 PHP 的一...

  • freemarker 模板

    freemarker 模板 目录 简介 文件结构 参考 简介 Android Studio 提供了不少模板,包括 ...

  • 003-搭建个人博客参考

    1、写在前面 在此记录,以备遗忘 2、博客模板 2.1、模板列表 地址1:http://demo.qfpffmp....

  • 大数据技术之Hadoop(二)

    Hadoop 运行环境搭建 2.1 模板 虚拟机 环境准备 0 ) 安装模板虚拟机,IP 地址 192.16...

  • GeekBand STL与泛型编程 第一周

    1.模板观念与函数模板 课程主要内容 C++模板简介 泛型编程 容器 进阶 C++模板简介   generic t...

  • 2.1 android IPC 简介

    1. 进程和线程的区别 线程是cpu调度的最小单元;而进程一般指一个执行单元。在pc和移动设备上。一个进程指一个应...

  • 3.8 Django 模板之 Template(1)

    生命就应该不自量力地还手,直至死方休 目录来了 模板介绍1.1 模板处理1.2 快捷函数 模板语言DTL2.1 变...

  • Collection接口和AbstractCollection

    目录简介 Collection接口 AbstractCollection抽象类2.1 简介2.2 为什么Abstr...

网友评论

      本文标题:2.1 模板简介

      本文链接:https://www.haomeiwen.com/subject/oslssctx.html