|
JsonTemplateExamples
Please put links to examples of using JSON Template here.
Using more_formattersThe 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)
|
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.
Something to consider: The JS code above trows a ugly error if the variable (foo) is not available.
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: ""});