<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NinjaCipher &#187; tornado</title>
	<atom:link href="http://www.ninjacipher.com/category/development/python/tornado/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ninjacipher.com</link>
	<description>kungpow programming</description>
	<lastBuildDate>Thu, 25 Mar 2010 14:39:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='www.ninjacipher.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Using Mako with Tornado Web Server</title>
		<link>http://www.ninjacipher.com/2010/01/01/using-mako-with-tornado-web-server/</link>
		<comments>http://www.ninjacipher.com/2010/01/01/using-mako-with-tornado-web-server/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 19:42:54 +0000</pubDate>
		<dc:creator>mattd</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[real time]]></category>
		<category><![CDATA[tornado]]></category>

		<guid isPermaLink="false">http://www.ninjacipher.com/?p=191</guid>
		<description><![CDATA[So at work we have been focusing on real time web services a lot lately. Due to this I&#8217;ve been getting a chance to play with some awesome new technologies that really lend themselves to the real time web. One of these new technologies is the Tornado Web Server.
&#8220;Tornado is an open source version of [...]]]></description>
			<content:encoded><![CDATA[<p>So <a href="http://wiredset.com" target="wiredset">at work</a> we have been focusing on real time web services a lot lately. Due to this I&#8217;ve been getting a chance to play with some awesome new technologies that really lend themselves to the real time web. One of these new technologies is the <a href="http://www.tornadoweb.org/" target="tornado">Tornado Web Server</a>.</p>
<blockquote><p>&#8220;Tornado is an open source version of the scalable, non-blocking web server and tools that power FriendFeed&#8221;</p></blockquote>
<p>We&#8217;re using <a href="http://www.makotemplates.org/" target="mako">Mako</a> to handle our templates (vs the template module that comes with Tornado). It&#8217;s super fast and feature rich and has good documentation. Bellow is a base class I wrote that illustrates how to render Mako templates from a Tornado RequestHandler class. It takes care of setting your template directory, template cache directory and your output encoding (utf-8). </p>
<p><strong>Note: This example assumes that you have your template settings defined as tornado options but they could be very easily just hard coded in. </strong></p>
<pre name="code" class="python">from tornado.web import RequestHandler
from mako.template import Template
from mako.lookup import TemplateLookup
from tornado.options import options

class BaseRequest(RequestHandler):
    def __init__(self, application, request, transforms=None):
        RequestHandler.__init__(self, application, request, transforms)
        self.lookup = TemplateLookup(directories=[options.template_dir], module_directory=options.mako_modules_dir, output_encoding='utf-8', encoding_errors='replace')

    def render_template(self,template_name, **kwargs):
        new_template = self.lookup.get_template(template_name)
        self.write(new_template.render(**kwargs))</pre>
<p>Basically you would just derive your handlers from BaseRequest vs from RequestHandler and you will then be able to render your Mako templates via the render_template method.</p>
<p>Here is an example:</p>
<pre name="code" class="python">class ExampleHandler(BaseRequest):
    def get(self):
        self.render_template('example.html')</pre>
<p>Shoot me a comment and let me know if you have any questions. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.ninjacipher.com/2010/01/01/using-mako-with-tornado-web-server/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
