RamonTurboGears

Thursday, January 17, 2008

Changing the default template engine in Turbogears

By default TG uses "kid" as its templating language. To change that go to wiki20/config/app.cfg

and search for
# tg.defaultview = "kid". copy it down and change to antlr_string.
tg.defaultview = "antlr_string"

Change the extension of all your templates to ".st"

start your server again and lo and behold, you have a welcome.st greeting you at the browser:).

=====
If you are feeling a little naughty, try this:
1. in the plugin make the extesion as ".kid" instead of ".st"
2. But change everything else to use antlr string template.
3. Now your kid templates are rendered by AST.
4. if you carefully chose py:replace and py:strip attributes, you can enclose your AST syntax in that.
5. Now when kid processes those templates, you dont see any of your AST code, when AST processes, you might see some jargon, but you can be careful and test on both engines and get good output :).

==
What you lose in this process is a strict enforcement of well formedness etc..
But what you gain is a clear line of separation between controller and view.

The only "disadvantage" if any is that the controller should know before hand what the keys used in the template are.

installing the plugin

now edit setup.py to look like the following.

then follow the standard python setup.py config, build and install --prefix .

After this step if you try tg-admin info at the command line, you should see something like the following in the output [ your versions may vary]:

Template Engines

* genshi-markup (Genshi 0.4.1)
* genshi-text (Genshi 0.4.1)
* genshi (Genshi 0.4.1)
* kid (TurboKid 1.0.1)
* cheetah (TurboCheetah 0.9.5)
* json (TurboJson 1.0)
* mako (Mako 0.1.6)
* antlr_string (RamBuffetString 0.1.1)

Now there is only one final step to start using ANTLR StringTemplate to create the view for your new website or wiki.












Pasted as Python by ramontg [ Remove line numbers | Show as plain text | Create new paste ]
Description: setup.py for antlrstringtemplate
URL: http://rafb.net/p/3GEE1916.html









1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from setuptools import setup, find_packages

setup(
name="RamBuffetString",
version="0.1.1",
description="Python plugin for ANTLR StringTemplate",
author="Sriram Durbha",
author_email="sriram.durbha@gmail.com",
url="http://ramontg.blogspot.com",
#scripts = [],

packages=find_packages(),
zip_safe=False,
entry_points = """
[python.templating.engines]
antlr_string = buffetstring.stringsupport:AntlrStringTemplatePlugin
"
""
)

interfacing ANTLR StringTemplate and TurboGears

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










Pasted as Python by ramontg [ Remove line numbers | Show as plain text | Create new paste ]
Description: Plugin to use ANTLR string template on turbogears.
URL: http://rafb.net/p/ZmYxfJ83.html









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):
# all template plugins need to define a default file extension

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
# check to see if we were passed a function get extra vars

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)

python version of stringtemplate

This post details how to install it so that you can write local python programs and process your own templates.

get the latest stringtemplate from stringtemplate.org . I have PyStringTemplate-3.1b1
make sure you note the dependencies properly.

the numbers on the left are from my history list. don't type those!!

1091 wget http://antlr2.org/download/antlr-2.7.7.tar.gz
1093 tar xzf antlr-2.7.7.tar.gz

1131 cd antlr-2.7.7/lib/python

1138 ls
1139 python setup.py config
1140 python setup.py build
1141 python setup.py install --prefix=/local/local/apps/python/
1142 p
1143 cd ../../../stringtemplate3-3.1b1/
1144 l
1145 python setup.py config
1146 python setup.py build
1147 python setup.py install --prefix=/local/local/apps/python/

Thats all there is to it. now you can try all the cool code on the stringtemplate.org site.