My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
JsonTemplateExamples  
Please put links to examples of using JSON Template here.
Updated Sep 27, 2009 by gtempacc...@yahoo.com

Using more_formatters

The more_formatters constructor argument lets you define a function which returns more formatting functions (in Java, these are classes).

On the Reference page, we've defined the names for standard formatters. But these are just reserved names -- specific implementations may or may not provide them by default.

In Python and JavaScript, the json / js-string formatters aren't defined by default because they require a dependency on more code which people may or may not want. To hook them up, you will need json2.js (or equivalent) in JavaScript and simplejson or equivalent in Python.

Examples:

JavaScript:

function more_formatters(formatter_name) {
  if formatter_name === 'json' || formatter_name === 'js-string' {
    return JSON.stringify;
  } else {
    return null;
  }
}

t = jsontemplate.Template("{foo|js-string}", {more_formatters: more_formatters});

Python:

def more_formatters(formatter_name):
  if formatter_name == 'json' or formatter_name == 'js-string':
    return simplejson.dumps
  else:
    return None

t = jsontemplate.Template("{foo|js-string}", more_formatters=more_formatters)
Comment by spydre...@yahoo.co.uk, Jan 25, 2010

Would like to use this with google app engine - how do i load my html template from the file system? or how do i pass the file address into json_template? Could you some file examples as this would be the best route for this.

Comment by thomas%f...@gtempaccount.com, Jul 9, 2010

Something to consider: The JS code above trows a ugly error if the variable (foo) is not available.

Comment by bharbu...@gmail.com, Jan 13, 2012

To prevent the error when foo is not available, you can used the undefined_str option, for example:

t = jsontemplate.Template("{foo|js-string}", {more_formatters: more_formatters, undefined_str: ""});
Powered by Google Project Hosting