| 1 |
.. _templates: |
| 2 |
|
| 3 |
Templating in Pecan |
| 4 |
=================== |
| 5 |
|
| 6 |
Pecan includes support for a variety of templating engines and also |
| 7 |
makes it easy to add support for new template engines. Currently, |
| 8 |
Pecan supports: |
| 9 |
|
| 10 |
=============== ============= |
| 11 |
Template System Renderer Name |
| 12 |
=============== ============= |
| 13 |
Mako_ mako |
| 14 |
Genshi_ genshi |
| 15 |
Kajiki_ kajiki |
| 16 |
Jinja2_ jinja |
| 17 |
JSON json |
| 18 |
=============== ============= |
| 19 |
|
| 20 |
.. _Mako: http://www.makotemplates.org/ |
| 21 |
.. _Genshi: http://genshi.edgewall.org/ |
| 22 |
.. _Kajiki: http://kajiki.pythonisito.com/ |
| 23 |
.. _Jinja2: http://jinja.pocoo.org/ |
| 24 |
|
| 25 |
The default template system is ``mako``, but that can be changed by |
| 26 |
passing the ``default_renderer`` key in your application's |
| 27 |
configuration:: |
| 28 |
|
| 29 |
app = { |
| 30 |
'default_renderer' : 'kajiki', |
| 31 |
# ... |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
Using Template Renderers |
| 36 |
------------------------ |
| 37 |
|
| 38 |
:py:mod:`pecan.decorators` defines a decorator called |
| 39 |
:func:`~pecan.decorators.expose`, which is used to flag a method as a public |
| 40 |
controller. The :func:`~pecan.decorators.expose` decorator takes a ``template`` |
| 41 |
argument, which can be used to specify the path to the template file to use for |
| 42 |
the controller method being exposed. |
| 43 |
|
| 44 |
:: |
| 45 |
|
| 46 |
class MyController(object): |
| 47 |
@expose('path/to/mako/template.html') |
| 48 |
def index(self): |
| 49 |
return dict(message='I am a mako template') |
| 50 |
|
| 51 |
:func:`~pecan.decorators.expose` will use the default template engine unless |
| 52 |
the path is prefixed by another renderer name. |
| 53 |
|
| 54 |
:: |
| 55 |
|
| 56 |
@expose('kajiki:path/to/kajiki/template.html') |
| 57 |
def my_controller(self): |
| 58 |
return dict(message='I am a kajiki template') |
| 59 |
|
| 60 |
.. seealso:: |
| 61 |
|
| 62 |
* :ref:`pecan_decorators` |
| 63 |
* :ref:`pecan_core` |
| 64 |
* :ref:`routing` |
| 65 |
|
| 66 |
|
| 67 |
Overriding Templates |
| 68 |
-------------------- |
| 69 |
|
| 70 |
:func:`~pecan.core.override_template` allows you to override the template set |
| 71 |
for a controller method when it is exposed. When |
| 72 |
:func:`~pecan.core.override_template` is called within the body of the |
| 73 |
controller method, it changes the template that will be used for that |
| 74 |
invocation of the method. |
| 75 |
|
| 76 |
:: |
| 77 |
|
| 78 |
class MyController(object): |
| 79 |
@expose('template_one.html') |
| 80 |
def index(self): |
| 81 |
# ... |
| 82 |
override_template('template_two.html') |
| 83 |
return dict(message='I will now render with template_two.html') |
| 84 |
|
| 85 |
Manual Rendering |
| 86 |
---------------- |
| 87 |
|
| 88 |
:func:`~pecan.core.render` allows you to manually render output using the Pecan |
| 89 |
templating framework. Pass the template path and values to go into the |
| 90 |
template, and :func:`~pecan.core.render` returns the rendered output as text. |
| 91 |
|
| 92 |
:: |
| 93 |
|
| 94 |
@expose() |
| 95 |
def controller(self): |
| 96 |
return render('my_template.html', dict(message='I am the namespace')) |
| 97 |
|
| 98 |
.. _expose_json: |
| 99 |
|
| 100 |
The JSON Renderer |
| 101 |
----------------- |
| 102 |
|
| 103 |
Pecan also provides a ``JSON`` renderer, which you can use by exposing |
| 104 |
a controller method with ``@expose('json')``. |
| 105 |
|
| 106 |
.. seealso:: |
| 107 |
|
| 108 |
* :ref:`jsonify` |
| 109 |
* :ref:`pecan_jsonify` |
| 110 |
|
| 111 |
|
| 112 |
Defining Custom Renderers |
| 113 |
------------------------- |
| 114 |
|
| 115 |
To define a custom renderer, you can create a class that follows the |
| 116 |
renderer protocol:: |
| 117 |
|
| 118 |
class MyRenderer(object): |
| 119 |
def __init__(self, path, extra_vars): |
| 120 |
''' |
| 121 |
Your renderer is provided with a path to templates, |
| 122 |
as configured by your application, and any extra |
| 123 |
template variables, also as configured |
| 124 |
''' |
| 125 |
pass |
| 126 |
|
| 127 |
def render(self, template_path, namespace): |
| 128 |
''' |
| 129 |
Lookup the template based on the path, and render |
| 130 |
your output based upon the supplied namespace |
| 131 |
dictionary, as returned from the controller. |
| 132 |
''' |
| 133 |
return str(namespace) |
| 134 |
|
| 135 |
|
| 136 |
To enable your custom renderer, define a ``custom_renderers`` key in |
| 137 |
your application's configuration:: |
| 138 |
|
| 139 |
app = { |
| 140 |
'custom_renderers' : { |
| 141 |
'my_renderer' : MyRenderer |
| 142 |
}, |
| 143 |
# ... |
| 144 |
} |
| 145 |
|
| 146 |
...and specify the renderer in the :func:`~pecan.decorators.expose` method:: |
| 147 |
|
| 148 |
class RootController(object): |
| 149 |
|
| 150 |
@expose('my_renderer:template.html') |
| 151 |
def index(self): |
| 152 |
return dict(name='Bob') |