模板

Pyramid includes adapters for two template engines, Mako and Chameleon. Mako是 Pylons 的默认引擎,所以它很熟悉。第三方适配器可用于其他引擎: "pyramid_jinja2" (Jinja2适配器)、“ Pyramid "pyramid_chameleon_gensi"(部分 Genshi 仿真器)等。

Mako configuration

要在 Pylons 中使用Mako,必须在设置中指定模板搜索路径:

[app:main]
mako.directories = pyramidapp:templates

This enables relative template paths like renderer="/mytemplate.mak" and quasi-URL paths like renderer="/mytemplate.mak" . It also allows templates to inherit from other templates, import other templates, and include other templates. Without this setting, the renderer arg will have to be in asset spec syntax, and templates won't be able to invoke other templates.

All settings with the "mako." prefix are passed to Mako's TemplateLookup 构造函数。例如。,

1mako.strict_undefined = true
2mako.imports =
3     from mypackage import myfilter
4mako.filters = myfilter
5mako.module_directory = %(here)s/data/templates
6mako.preprocessor = mypackage.mako_preprocessor

以“.mak”或“.mako”结尾的模板文件名将发送到mako渲染器。如果您喜欢其他扩展名,如“.html”,可以将其放入主函数:

config.add_renderer(".html", "pyramid.mako_templating.renderer_factory")

If you have further questions about exactly how the Mako renderer is implemented, it's best to look at the source: pyramid.mako_templating . 您可以将其与mako文档进行协调,以确认什么参数值导致了什么。

*注意:*当我在一个没有配置信标测试的应用程序中将“mako.strict_Undefined”设置为true时,它破坏了调试工具栏。工具栏模板可能有一些不受“%if”保护的草率占位符。

*警告2:*假设您可以通过一个资产规范而不是模板路径,但我不能让它工作。

Chameleon

Chameleon 是一种基于XML的模板语言,源于Zope。它与根氏有一些相似之处。其文件扩展名为.pt(“页面模板”)。

Chameleon 的优点:

  • 基于XML的语法。

  • 模板必须是格式良好的XHTML,这表明(但不保证)输出将是格式良好的。如果任何变量占位符标记为“structure”,则可以在模板中插入无效的XML。

  • Good internationalization support in Pyramid.

  • 速度和Mako一样快。(XML模板语言不常见。)

  • 占位符语法“$varname or expression”是 Chameleon 、Mako和Genshi的常见语法。

  • Chameleon 有一个接受非XML输入的文本模式,但是您会丢失除“$varname”之外的所有控制结构。

Disadvantages of Chameleon:

  • 基于XML的语法。

  • 文件名必须采用资产规范语法,而不是相对路径: renderer="mypackage:templates/foo.pt"renderer="templates/foo.pt" . You can't get rid of that "templates/" prefix without writing a wrapper view_config 装饰者。

  • 没有模板查找,因此在没有将模板预加载到变量中之前,不能从另一个模板内部调用一个模板。

  • 如果模板不是格式良好的XML,那么用户将得到一个无条件的“内部服务器错误”,而不是在浏览器中看起来很好并且用户至少可以从中读取某些内容的错误。

  • 它不适用于所有的平台,Mako和Pyramid做的。(仅限CPython和Google应用程序引擎。)

Renderer globals

每当渲染器调用模板时,模板命名空间包含视图返回dict中的所有变量,以及以下系统变量:

request, req

The current request.

view

The view instance (for class-based views) or function (for function-based views). You can read instance attributes directly: view.foo .

context

The context (same as request.context )(在mako中不可见,因为mako有一个具有此名称的内置变量;请使用 request.context 取而代之的是)

renderer_name

完全限定的渲染器名称;例如,“zzz:templates/foo.mako”。

renderer_info

具有属性的对象 namepackagetype .

Akhet演示演示如何将其他变量注入到所有模板中,例如帮助器模块 h , a URL generator url , the session variable session 等。

网站模板

大多数网站将使用网站模板与页面模板相结合,以确保所有页面具有相同的外观(页眉、侧栏和页脚)。Mako的继承使页面模板从网站模板继承变得容易。这是一个非常简单的网站模板:

 1<!DOCTYPE html>
 2<html>
 3  <head>
 4    <title>My Application</title>
 5  </head>
 6  <body>
 7
 8<!-- *** BEGIN page content *** -->
 9${self.body()}
10<!-- *** END page content ***-->
11
12  </body>
13</html>

…and a page template that uses it:

1<%inherit file="/site.html" />
2
3<p>
4  Welcome to <strong>${project}</strong>, an application ...
5</p>

A more elaborate example is in the Akhet demo.