Export to GitHub

circuits - HowToCircuitsWebBasicTemplating.wiki


summary HowTo (circuits.web): Basic Templating

Basic Templating

circuits.web tries to stay out of your way as much as possible and doesn't impose any restrictions on what external libraries and tools you can use throughout your web application or website. As such you can use any templating language/engine you wish.

Using Mako

This basic example of using the Mako Templating Language. First a TemplateLookup instance is created. Finally a function called render(name, **d) is created that is used by Request Handlers to render a given template and apply data to it.

Here is the basic example:

```

!/usr/bin/env python

import os

import mako from mako.lookup import TemplateLookup

from circuits.web import Server, Controller

templates = TemplateLookup( directories=[os.path.join(os.path.dirname(file), "tpl")], module_directory="/tmp", output_encoding="utf-8")

def render(name, **d): try: return templates.get_template(name).render(**d) except: return mako.exceptions.html_error_template().render()

class Root(Controller):

def index(self):
    return render("index.html")

def submit(self, firstName, lastName):
    msg = "Thank you %s %s" % (firstName, lastName)
    return render("index.html", message=msg)

(Server(8000) + Root()).run() ```


HowTo Guides