<div dir="ltr"><div><div><div><div>This is great, thanks.<br><br></div></div></div>I want to deploy a content server on my Raspberry Pi, do I need to write a separate file for the server/reactor that imports views.py and config.py?<br>
<br></div>Jo <br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Jun 19, 2013 at 2:35 PM, Lucas Taylor <span dir="ltr"><<a href="mailto:ltaylor.volks@gmail.com" target="_blank">ltaylor.volks@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div class="h5"><br><div><div>On Jun 18, 2013, at 8:47 PM, Jo as Queeniebee wrote:</div>
<br><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" target="_blank">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><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" target="_blank">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></div><br>_______________________________________________<br>

Twisted-web mailing list<br>
<a href="mailto:Twisted-web@twistedmatrix.com">Twisted-web@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web</a><br>
<br></blockquote></div><br></div>