tag:blogger.com,1999:blog-42547005191230541382024-02-20T06:32:12.782-08:00RamonTurboGearsRamhttp://www.blogger.com/profile/03503692967468323994noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-4254700519123054138.post-52410190897193186122008-01-17T15:36:00.000-08:002008-01-17T15:46:36.386-08:00Changing the default template engine in TurbogearsBy default TG uses "kid" as its templating language. To change that go to wiki20/config/app.cfg<br /><br />and search for<br /># tg.defaultview = "kid". copy it down and change to antlr_string.<br /> tg.defaultview = "antlr_string"<br /><br />Change the extension of all your templates to ".st"<br /><br />start your server again and lo and behold, you have a welcome.st greeting you at the browser:).<br /><br />=====<br />If you are feeling a little naughty, try this:<br />1. in the plugin make the extesion as ".kid" instead of ".st"<br />2. But change everything else to use antlr string template.<br />3. Now your kid templates are rendered by AST.<br />4. if you carefully chose py:replace and py:strip attributes, you can enclose your AST syntax in that.<br />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 :).<br /><br />==<br />What you lose in this process is a strict enforcement of well formedness etc..<br />But what you gain is a clear line of separation between controller and view.<br /><br />The only "disadvantage" if any is that the controller should know before hand what the keys used in the template are.Ramhttp://www.blogger.com/profile/03503692967468323994noreply@blogger.com0tag:blogger.com,1999:blog-4254700519123054138.post-77475401033112583382008-01-17T15:29:00.000-08:002008-01-17T15:36:24.599-08:00installing the pluginnow edit setup.py to look like the following.<br /><br />then follow the standard python setup.py config, build and install --prefix .<br /><br />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]:<br /><br />Template Engines<br /><br />* genshi-markup (Genshi 0.4.1)<br />* genshi-text (Genshi 0.4.1)<br />* genshi (Genshi 0.4.1)<br />* kid (TurboKid 1.0.1)<br />* cheetah (TurboCheetah 0.9.5)<br />* json (TurboJson 1.0)<br />* mako (Mako 0.1.6)<br />* antlr_string (RamBuffetString 0.1.1)<br /><br />Now there is only one final step to start using ANTLR StringTemplate to create the view for your new website or wiki.<br /><br /><div style="border-bottom: 2px solid gray;"><table border="0" cellpadding="1" cellspacing="2" width="100%"><br /><tbody><tr><br /> <td><small>Pasted as <b>Python</b> by <b>ramontg</b> <span id="controls">[ <a href="http://www.blogger.com/p/3GEE1916.nln.html">Remove line numbers</a> | <a href="http://www.blogger.com/p/3GEE1916.txt">Show as plain text</a> | <a href="http://www.blogger.com/paste/">Create new paste</a> ]</span></small></td><br /><br /></tr><br /><tr><br /> <td><small>Description: setup.py for antlrstringtemplate</small></td><br /></tr><br /><tr><br /> <td><small>URL: http://rafb.net/p/3GEE1916.html</small></td><br /></tr><br /></tbody></table><br /></div><br /><br /><table border="0" cellpadding="1" cellspacing="2"><br /><tbody><tr><br /> <td><pre class="code">1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17</pre></td><br /><br /> <td width="100%"><pre class="code" id="codemain"><span class="keyword">from</span> setuptools <span class="keyword">import</span> setup, find_packages<br /><br />setup(<br /> name=<span class="literal">"RamBuffetString"</span>,<br /> version=<span class="literal">"0.1.1"</span>,<br /> description=<span class="literal">"Python plugin for ANTLR StringTemplate"</span>,<br /> author=<span class="literal">"Sriram Durbha"</span>,<br /> author_email=<span class="literal">"sriram.durbha@gmail.com"</span>,<br /> url=<span class="literal">"http://ramontg.blogspot.com"</span>,<br /> <span class="comment">#scripts = [],<br /><br /></span> packages=find_packages(),<br /> zip_safe=False,<br /> entry_points = <span class="literal">""</span><span class="literal">"<br /> [python.templating.engines]<br /> antlr_string = buffetstring.stringsupport:AntlrStringTemplatePlugin<br /> "</span><span class="literal">""</span><br /> )</pre></td><br /></tr><br /></tbody></table>Ramhttp://www.blogger.com/profile/03503692967468323994noreply@blogger.com0tag:blogger.com,1999:blog-4254700519123054138.post-63338237794743994052008-01-17T15:24:00.000-08:002008-01-17T15:29:54.813-08:00interfacing ANTLR StringTemplate and TurboGears1. Follow previous post to install everything.<br /><br />Download http://projects.dowski.com/files/buffetstring/BuffetString-0.1.1.zip<br />unzip and edit the file buffetstring/stringsupport.py to look like the following.<br /><br />Then follow the next post to change setup.py<br /><div style="border-bottom: 2px solid gray;"><table border="0" cellpadding="1" cellspacing="2" width="100%"><tbody><tr><br /> <td><small>Pasted as <b>Python</b> by <b>ramontg</b> <span id="controls">[ <a href="http://www.blogger.com/p/ZmYxfJ83.nln.html">Remove line numbers</a> | <a href="http://www.blogger.com/p/ZmYxfJ83.txt">Show as plain text</a> | <a href="http://www.blogger.com/paste/">Create new paste</a> ]</span></small></td><br /><br /></tr><br /><tr><br /> <td><small>Description: Plugin to use ANTLR string template on turbogears.</small></td><br /></tr><br /><tr><br /> <td><small>URL: http://rafb.net/p/ZmYxfJ83.html</small></td><br /></tr><br /></tbody></table><br /></div><br /><br /><table border="0" cellpadding="1" cellspacing="2"><br /><tbody><tr><br /> <td><pre class="code">1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63</pre></td><br /><br /> <td width="100%"><pre class="code" id="codemain"><span class="literal">""</span><span class="literal">"<br />BuffetString is more of a proof-of-concept and example<br />'python.templating.engines' plugin than anything else. If it is useful<br />to you, great. Otherwise, just read over the source for a simple example<br />of making a plugin.<br />"</span><span class="literal">""</span><br /><br /><span class="keyword">import</span> string<br /><span class="keyword">import</span> os<br /><span class="keyword">import</span> stringtemplate3<br /><br /><span class="keyword">class </span>AntlrStringTemplatePlugin(object):<br /> <span class="comment"># all template plugins need to define a default file extension<br /><br /></span> extension = <span class="literal">".st"</span><br /><br /> <span class="keyword">def</span> __init__(self, extra_vars_func=None, config=None):<br /> <span class="literal">""</span><span class="literal">"extra_vars_func == optional callable() that returns a dict<br /> config == optional dict() of configuration settings<br /> "</span><span class="literal">""</span><br /> self.get_extra_vars = extra_vars_func<br /> <span class="keyword">if</span> config:<br /> self.config = config<br /> <span class="keyword">else</span>:<br /> self.config = dict()<br /><br /><br /> <span class="keyword">def</span> load_template(self, template_name):<br /> <span class="literal">""</span><span class="literal">"template_name == dotted.path.to.template (without .ext)<br /><br /> The dotted notation is present because many template engines<br /> allow templates to be compiled down to Python modules. TurboGears<br /> uses that feature to its adavantage, and for ease of integration<br /> the python.templating.engines plugin format requires the path to<br /> the template to be supplied as a dotted.path.to.template regardless<br /> of whether is is a module or not.<br /><br /> In the case of string.Template templates, they are just simple text<br /> files, so we deal with the dotted notation and translate it into a<br /> standard file path to open the text file.<br /> "</span><span class="literal">""</span> <br /> parts = template_name.split(<span class="literal">'.'</span>)<br /> template_name = <span class="literal">"%s"</span> % (parts.pop())<br /> template_path = os.path.join(*parts)<br /> self.group = stringtemplate3.StringTemplateGroup(<span class="literal">"myGroup"</span>, template_path)<br /> template_obj = self.group.getInstanceOf(template_name)<br /> <span class="keyword">return</span> template_obj<br /><br /><br /> <span class="keyword">def</span> render(self, info, format=<span class="literal">"html"</span>, fragment=False, template=None):<br /> <span class="literal">""</span><span class="literal">"info == dict() of variables to stick into the template namespace<br /> format == output format if applicable<br /> fragment == special rules about rendering part of a page<br /> template == dotted.path.to.template (without .ext)<br /><br /> You might not need all of these arguments. info and template are the<br /> only ones used in this simple example.<br /> "</span><span class="literal">""</span><br /> vars = info<br /> <span class="comment"># check to see if we were passed a function get extra vars<br /><br /></span> <span class="keyword">if</span> callable(self.get_extra_vars):<br /> vars.update(self.get_extra_vars())<br /> template_obj = self.load_template(template)<br /> <span class="keyword">for</span> key <span class="keyword">in</span> vars.keys():<br /> template_obj[key]=vars[key]<br /> <span class="keyword">return</span> str(template_obj)</pre></td><br /></tr><br /></tbody></table>Ramhttp://www.blogger.com/profile/03503692967468323994noreply@blogger.com0tag:blogger.com,1999:blog-4254700519123054138.post-62251420735290762572008-01-17T10:53:00.000-08:002008-01-17T15:22:00.943-08:00python version of stringtemplateThis post details how to install it so that you can write local python programs and process your own templates.<br /><br />get the latest stringtemplate from stringtemplate.org . I have <a href="http://fisheye2.cenqua.com/browse/stringtemplate/python/release/PyStringTemplate-3.1b1">PyStringTemplate-3.1b1</a><br />make sure you note the dependencies properly.<br /><br />the numbers on the left are from my history list. don't type those!!<br /><br />1091 wget http://antlr2.org/download/antlr-2.7.7.tar.gz<br />1093 tar xzf antlr-2.7.7.tar.gz<br /><br />1131 cd antlr-2.7.7/lib/python<br /><br />1138 ls<br />1139 python setup.py config<br />1140 python setup.py build<br />1141 python setup.py install --prefix=/local/local/apps/python/<br />1142 p<br />1143 cd ../../../stringtemplate3-3.1b1/<br />1144 l<br />1145 python setup.py config<br />1146 python setup.py build<br />1147 python setup.py install --prefix=/local/local/apps/python/<br /><br />Thats all there is to it. now you can try all the cool code on the stringtemplate.org site.Ramhttp://www.blogger.com/profile/03503692967468323994noreply@blogger.com0