<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 TwistedMatrix.com 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? This seems a bit inefficient when you have a file larger than <html><body> Hello, World!</body></html>.</div>
<div><br></div><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><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>