Log in

tipfy.ext.jsontemplate

This extension provides JSON Template templating support to tipfy.

Basic usage

handlers.py

from tipfy import Tipfy, RequestHandler
from tipfy.ext.jinja2 import render_response
from tipfy.ext import jsontemplate

SINGERS = [
  {"name": "T-Bone Walker", "tags": ["gee", "baby"]},
  {"name": "John Hooker"},
  {"name": "Janis Joplin", "tags": ["piece", "heart"]}, 
]

class BluesHandler(RequestHandler):
    def get(self):
        return render_response('blues.html',
            singers=SINGERS,
            render=jsontemplate.get_template("singer.jsont").expand,
        )

The handler uses Jinja2 to render our main templates. We want to display contents from the SINGERS list.

templates/blues.html

<html>
    <body>
        <h1>Singers:</h1>
        {% for singer in singers %}
        {{ render(object=singer) }}
        {% endfor %}
    </body>
</html>

The main template renders each singer with the function render() which was provided by the handler.

templates/json/singer.jsont

{.section object}
  <h2>{name}</h2>
  <ul>
  {.repeated section tags} 
    <li>{@}</li>
  {.end}
  </ul>
{.end}

This is the JSON Template, which is used by render().

Result:

Singers:

T-Bone Walker
    * gee
    * baby

John Hooker

Janis Joplin
    * piece
    * heart

Configuration

config.py

config['tipfy.ext.jsontemplate'] = {
    'templates_dir': 'templates/json',
}

Javascript

See http://code.google.com/p/json-template/ documentation for client-side rendering with javascript.

Usage patterns

Ajax + JSON Templates

  • the server renders page layout with <your templating language here> and page objects with jsontemplate
  • the client requests updates via ajax, gets JSON data response
  • the client renders the data using the same jsontemplates that were used by the server

Extension reference

Links


Powered by Moe. Yeah, the name is Moe. Powered by Google App Engine