Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
11
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

11. TEMPLATES

5

6

7

def my_view(request):

main = get_renderer(’templates/master.pt’).implementation() return {’main’:main}

Where templates/master.pt might look like so:

1

2

3

4

5

6

7

8

9

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:tal="http://xml.zope.org/namespaces/tal" xmlns:metal="http://xml.zope.org/namespaces/metal">

<span metal:define-macro="hello">

<h1>

Hello <span metal:define-slot="name">Fred</span>!

</h1>

</span>

</html>

And templates/mytemplate.pt might look like so:

1

2

3

4

5

6

7

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:tal="http://xml.zope.org/namespaces/tal" xmlns:metal="http://xml.zope.org/namespaces/metal">

<span metal:use-macro="main.macros[’hello’]"> <span metal:fill-slot="name">Chris</span>

</span>

</html>

11.5 Templating with Chameleon Text Templates

Pyramid also allows for the use of templates which are composed entirely of non-XML text via Chameleon. To do so, you can create templates that are entirely composed of text except for ${name} -style substitution points.

Here’s an example usage of a Chameleon text template. Create a file on disk named mytemplate.txt in your project’s templates directory with the following contents:

Hello, ${name}!

Then in your project’s views.py module, you can create a view which renders this template:

130

11.6. SIDE EFFECTS OF RENDERING A CHAMELEON TEMPLATE

1

2

3

4

5

from pyramid.view import view_config

@view_config(renderer=’templates/mytemplate.txt’) def my_view(request):

return {’name’:’world’}

When the template is rendered, it will show:

Hello, world!

If you’d rather use templates directly within a view callable (without the indirection of using a renderer), see pyramid.chameleon_text for the API description.

See also Built-In Renderers for more general information about renderers, including Chameleon text renderers.

11.6 Side Effects of Rendering a Chameleon Template

When a Chameleon template is rendered from a file, the templating engine writes a file in the same directory as the template file itself as a kind of cache, in order to do less work the next time the template needs to be read from disk. If you see “strange” .py files showing up in your templates directory (or otherwise directly “next” to your templates), it is due to this feature.

If you’re using a version control system such as Subversion, you should configure it to ignore these files. Here’s the contents of the author’s svn propedit svn:ignore . in each of my templates directories.

*.pt.py *.txt.py

Note that I always name my Chameleon ZPT template files with a .pt extension and my Chameleon text template files with a .txt extension so that these svn:ignore patterns work.

11.7 Debugging Templates

A NameError exception resulting from rendering a template with an undefined variable (e.g. ${wrong}) might will end like this:

131

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]