<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi Jo,<div><br><div><div>On Jun 26, 2013, at 10:52 AM, Jo as Queeniebee wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr">Hello,<div><br></div><div>I've asked a question before about using Jinja2 with Twisted.Web, but now I have a more general question about rendering HTML docs within a Twisted web application.</div><div><br>
</div><div>All the examples on <a href="http://TwistedMatrix.com">TwistedMatrix.com</a> and the txTemplate adapter show that you pass the web page by either returning it as a string from render_Get or as a param in .render(). Is this true for all cases?</div></div></blockquote><div><br></div><div><div>A Resource should either directly return bytes from a render* method, OR, return twisted.web.server.NOT_DONE_YET to indicate an asynchronous response will be returned *at some point*.  </div><div><br></div><div>API:  <a href="http://twistedmatrix.com/documents/current/api/twisted.web.resource.IResource.html#render">http://twistedmatrix.com/documents/current/api/twisted.web.resource.IResource.html#render</a></div><div><br></div><div>Also, these howtos may be useful to clarify the concept:</div><div><a href="http://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous.html">http://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous.html</a></div><div><a href="http://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous-deferred.html">http://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous-deferred.html</a></div><div><br></div><div><br></div><div>If you're just loading up a template from disk and rendering it with local data, you may find that synchronous load/render/return works fine. If you find that you are blocking the web server from servicing other requests because you are querying a database, or fetching something from a remote service or something, then you'll want to go the asynchronous route.</div><div><br></div><div>(Note that in your code below, there is really no reason to do it asynchronously)</div><div><br></div><div><br></div></div><blockquote type="cite"><div dir="ltr"><div>This seems a bit inefficient when you have a file larger than <html><body> Hello, World!</body></html>.</div></div></blockquote><div><br></div><div><div>If you are planning on streaming large amounts of data, then Twisted's producer/consumer api may be helpful:</div><div><a href="http://twistedmatrix.com/documents/current/core/howto/producers.html">http://twistedmatrix.com/documents/current/core/howto/producers.html</a></div><div><br></div><div><br></div><div><br></div></div><blockquote type="cite"><div dir="ltr"><div>I've written an HTML file complete with .css and .js. How to I pass that document to my Twisted.Web resource? When I use the code below, the context replaces all markup from my HTML file. How do I get around this? How do you pass a larger HTML file to Twisted.Web?</div>
<div><br></div></div></blockquote><blockquote type="cite"><div dir="ltr"><div><div># -*- coding: utf-8 -*</div><div><br></div><div>import os, sys</div><div><br></div><div>from twisted.application import internet</div><div>from twisted.web.resource import Resource</div><div>from twisted.web import server</div>
<div>from twisted.internet import reactor</div><div><br></div><div>import txtemplate</div><div><br></div><div>TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),"templates")</div><div><br></div>
<div>class ThreadResource(Resource):</div><div>    def __init__(self):</div><div>        resource.Resource.__init__(self)</div><div>        self.loader = txtemplate.Jinja2TemplateLoader(TEMPLATE_DIR)</div><div>    </div><div>
    def getChild(self, name, request):</div><div>         return self</div><div><span class="" style="white-space:pre">    </span></div><div>    def render_GET(self, request):</div><div>    <span class="" style="white-space:pre">  </span>template_name = "base.html"</div>
<div>        template = self.loader.load(template_name)</div><div>        context = {"greeting": "Enter"}</div><div> </div><div>        def cb(content):</div><div>            request.write(content)</div>
<div>            request.setResponseCode(200)</div><div>            request.finish()</div><div> </div><div>    d = template.render(**context)</div><div>        d.addCallback(cb)</div><div>        return server.NOT_DONE_YET</div>
</div><div><div><br></div><div>site = server.Site(ThreadResource())</div><div>reactor.listenTCP(8888, site)</div><div>reactor.run()</div></div></div>
_______________________________________________<br></blockquote><br></div><div><br></div><div><br></div><div>Your code looks fine (w/ a minor fixup in __init__).  I did a quick test using your code and the context rendered fine. e.g. in 'base.html' I just have:</div><div><div><br></div><div>Greeting:  {{ greeting }}</div><div><br></div><div>Maybe your template is malformed? </div><div><br></div><div><br></div><div><br></div></div><div>Lucas</div></div><div><br></div><div><br></div><div><br></div></body></html>