1. Follow previous post to install everything.
Download http://projects.dowski.com/files/buffetstring/BuffetString-0.1.1.zip
unzip and edit the file buffetstring/stringsupport.py to look like the following.
Then follow the next post to change setup.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
""" BuffetString is more of a proof-of-concept and example 'python.templating.engines' plugin than anything else. If it is useful to you, great. Otherwise, just read over the source for a simple example of making a plugin. """
import string import os import stringtemplate3
class AntlrStringTemplatePlugin(object): extension = ".st"
def __init__(self, extra_vars_func=None, config=None): """extra_vars_func == optional callable() that returns a dict config == optional dict() of configuration settings """ self.get_extra_vars = extra_vars_func if config: self.config = config else: self.config = dict()
def load_template(self, template_name): """template_name == dotted.path.to.template (without .ext)
The dotted notation is present because many template engines allow templates to be compiled down to Python modules. TurboGears uses that feature to its adavantage, and for ease of integration the python.templating.engines plugin format requires the path to the template to be supplied as a dotted.path.to.template regardless of whether is is a module or not.
In the case of string.Template templates, they are just simple text files, so we deal with the dotted notation and translate it into a standard file path to open the text file. """ parts = template_name.split('.') template_name = "%s" % (parts.pop()) template_path = os.path.join(*parts) self.group = stringtemplate3.StringTemplateGroup("myGroup", template_path) template_obj = self.group.getInstanceOf(template_name) return template_obj
def render(self, info, format="html", fragment=False, template=None): """info == dict() of variables to stick into the template namespace format == output format if applicable fragment == special rules about rendering part of a page template == dotted.path.to.template (without .ext)
You might not need all of these arguments. info and template are the only ones used in this simple example. """ vars = info if callable(self.get_extra_vars): vars.update(self.get_extra_vars()) template_obj = self.load_template(template) for key in vars.keys(): template_obj[key]=vars[key] return str(template_obj) |
No comments:
Post a Comment