Home Blog CV Projects Patterns Notes Book Colophon Search

Pylons: Mako Templates in AuthKit

14 Aug, 2007

AuthKit's form and openid methods take a authkit.form.template.obj or authkit.openid.template.obj argument which is a Paste import string to a function which returns a template. This is very handy if you want AuthKit to render a sign in page which looks the same as the rest of your site because you can use your existing templates. Here's how...

If your project was named myproject you could create a file in myproject/lib/auth.py which looks like this:

import pylons
from pylons.templating import Buffet
from pylons import config
import myproject.lib.helpers as h

class MyBuffet(Buffet):
    def _update_names(self, ns):
        return ns

def_eng = config['buffet.template_engines'][0]
buffet = MyBuffet(
    def_eng['engine'],
    template_root=def_eng['template_root'],
    **def_eng['template_options']
)

for e in config['buffet.template_engines'][1:]:
    buffet.prepare(
        e['engine'],
        template_root=e['template_root'],
        alias=e['alias'],
        **e['template_options']
    )

class State:
    pass

c = State()
c.user = 'None'

def make_template():
    return buffet.render(
        template_name="/core/derived/signin.mako",
        namespace=dict(h=h, c=State())
    ).replace("%", "%%").replace("FORM_ACTION", "%s")

There is quite a lot of boiler plate because you need to setup your own instance of buffet just for this rendering but it does all work. You can then set the AuthKit config option like this:

authkit.form.template.obj = myproject.lib.auth:make_template

Copyright James Gardner 1996-2020 All Rights Reserved. Admin.