PylonsHQ.

Layout: Fixed-width
  Dashboard > Pylons 0.9.6 Official Docs > Home > Using Other Template Languages
  Pylons 0.9.6 Official Docs Log In | Sign Up   View a printable version of the current page.  
  Using Other Template Languages
Added by James Gardner, last edited by Philip Jenvey on Aug 01, 2007  (view change) show comment
Labels: 
(None)

Template Language Plugins

Pylons supports a variety of template languages in addition to Mako through the use of template engine plug-ins. This can be useful both for migrating web applications to Pylons, or in cases where you just would rather prefer some other templating solution.

Template language plug-ins can be installed rather easily using setuptools. A current list of template engine plug-ins is at the Buffet website.

Once you have installed one of these, using the new template language within Pylons is quite easy. As Pylons does not come pre-configured with this in mind, you will need to do a little more work yourself depending on which template language you're using.

Example: Using Kid with Pylons

If you didn't select the kid optional extra package when you installed Pylons (described on the install page) you will need to install the appropriate Buffet plugin. In the case of Kid, this is called TurboKid and can be installed as follows:

1
$ easy_install TurboKid

To use Kid with Pylons, first we must setup a new template directory for the Kid templates.

First, create a directory in yourproject called kidtemplates and add a controller:

1
2
3
$ cd yourproject
$ mkdir yourproject/kidtemplates
$ paster controller kid

You will now have a kid.py controller in your controllers directory. First, we will need to add Kid to the available template engines.

Edit yourproject/config/environment.py add to the bottom of the load_environment function:

1
2
kidopts = {'kid.assume_encoding':'utf-8', 'kid.encoding':'utf-8'}
config.add_template_engine('kid', 'yourproject.kidtemplates', kidopts)

Edit the KidController class so it looks like this:

1
2
3
4
5
class KidController(BaseController):
    def index(self):
        c.title = "Your Page"
        c.message = 'hi'
        return render_response('kid', 'test')

Make sure to change yourproject.kidtemplates to reflect what your project is actually called. The first argument to render or render_response can be the template engine to use, while the second non-keyword argument is the template. If you don't specify a template engine, it will drop back to the default (Mako, unless you change the default).

Now let's add the Kid template to render, create the file yourproject/kidtemplates/test.kid with the following content:

1
2
3
4
5
6
7
8
9
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#">
    <head>
        <title py:content="c.title">title</title>
    </head>
    <body>
        <p py:content="c.message">message</p>
        <p>You made it to the following url: ${h.url_for()}</p>
    </body>
</html>

Since the template plug-ins currently expect paths to act as module imports, you will also need to create a __init__.py file inside yourproject/kidtemplates.

Loading /kid will now return the Kid template that you have created.

Notice that all the same Pylons variables are made accessible to template engine plug-ins. You will have c, h, g, session, and request available in any template language you choose to use. This also makes it easier to switch later to Mako or a different template language without having to update your controller action.

Switching the Default Template Engine

In Pylons, customization is not just allowed but actively encouraged. It's quite easy to change the default engine from Mako to your choice. Let's make Kid the default template engine.

Edit yourproject/config/environment.py and change the template_engine argument passed to config.init_app:

1
2
config.init_app(global_conf, app_conf, package='yourproject',
                template_engine='kid', paths=paths)

This swaps Mako out and uses Kid, making Kid the new default template engine. The above index method no longer needs to specify 'kid' now when rendering a template. The existing templates directory will be used, and you'll need to create the __init__.py file before adding Kid templates. Current template engine's that can be swapped in this manner are kid, mako, and genshi.

Note

For more details on the config object, check out the extensive Config docs from the Pylons Module API.

Using genshi allongside mako (in 0.9.6rc1)
==========================================

First you need to install Genshi, the usual way applies 'easy_install Genshi'.

Then you need to configure the genshi engine in your project, open yourproject/config/environment.py and add
>>>
config.add_template_engine('genshi', 'yourproject.templates', {})
<<<
at the end of the file.

Also, don't forget to create an empy _init.py in yourproject/templates/ so that genshi is certain it's a python package directory (this is required by Genshi 0.4.3 at least). A simple 'touch yourproject/templates/init_.py' is enough.

Then, if you have a yourproject/templates/serverinfo.html template, you can render it by
calling render('genshi', 'serverinfo')
This is equivalent to return render('mako', '/serverinfo.mak') or just return render('/serverinfo.mak') since mako is still default (we didn't change that).

Posted by Damjan Georgievski at Jul 24, 2007 19:57 | Permalink

Using genshi as the default templating engine in Pylons 0.9.6.1


Install genshi with Pylons by easy_install:

easy_install Pylons[genshi]

Now edit the init_app line in config/environment.py like so:

    config.init_app(global_conf, app_conf, package='mypackage',
                    template_engine='genshi', paths=paths)

As above, you need to create a blank _init_.py in any directories that contain genshi templates.

One last note is with regards to naming.  genshi templates are .html files, and they're rendered as if they were python packages.  So, if in your templates directory, you have a subdirectory called account, and you want a template called signup.html, your directory structure would be as follows:

    template/

    template/_init_.py

    template/account

    template/account/_init_.py

    template/account/signup.html

In your code, you would render this file like this:

render('account.signup')


Posted by Douglas Mayle at Mar 05, 2008 21:41 | Permalink
Site running on a free Atlassian Confluence Open Source Project License granted to Pylons. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.3.3 Build:#645 Feb 13, 2007) - Bug/feature request - Contact Administrators

Powered by Pylons - Contact Administrators