<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Jun 18, 2013, at 8:47 PM, Jo as Queeniebee wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr"><div><div><div><div>Hello,<br><br>I'm writing a content server to run on my Raspberry Pi and I'll be using Jinja2 as the templating engine. I'm new to Twisted so I'm having a hard time imaging the examples for <a href="http://TwistedMatrix.com">TwistedMatrix.com</a> scale. <br>
<br></div>Are there any examples of a fully featured Twisted application that uses Jinja2 or any other templating engine like Mako?<br></div></div></div></div></blockquote></div><br><div><br></div><div><div>Hi Jo,</div><div><br></div><div>You can return a string (e.g. a rendered jinja2 template) from the .render method of an IResource</div><div><a href="http://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html#auto6">http://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html#auto6</a></div><div><br></div><div>So when you see a string being returned from a Resource in the docs, you can consider substituting the particulars of the template engine you want to use.</div><div><br></div><div><br></div><div>Here's a very simple example of one approach using jinja2:</div><div><br></div><div>##  myapp/config.py</div><div>#</div><div># Define a jinja2 Environment somewhere</div><div># I specify a PackageLoader to look up templates from the templates folder in the myapp.web.static package</div><div>from jinja2 import Environment, PackageLoader</div><div>jinja = Environment(loader=PackageLoader('myapp.web.static', 'templates'))</div><div><br></div><div>----</div><div>## myapp/web/views.py</div><div>#</div><div>from twisted.web.resource import Resource</div><div>from myapp.config import jinja</div><div><br></div><div>class MyResource(Resource):</div><div>    isLeaf = True    </div><div>    template_name = "example.html"</div><div>    </div><div>    def render_GET(self, request):</div><div>        ctx = {</div><div>            'content': "Templated content",</div><div>            'extra_info': [1, 2, 3, 4, 5, 6]</div><div>        }</div><div>        template = jinja.get_template(self.template_name)</div><div>        </div><div>        return template.render(ctx)</div></div><div><br></div></body></html>