-
Notifications
You must be signed in to change notification settings - Fork 0
Use as WSGI Body Iterable
Using cinje template functions as WSGI body generators was a primary design concern. As most other template engines fully buffer output, use as a WSGI body requires additional wrapping in a list or other iterable. Cinje templates, being generators, do not require this.
If the WSGI server you are using is forgiving (accepts unicode bodies), you can actually use a cinje template as your whole WSGI application:
# encoding: cinje
: def app environ, start_response
: start_response('200 OK', [("Content-Type": "text/plain")])
Hello.
Because these template functions return unicode chunks, to be explicitly compatible with WSGI you'll need to convert these to byte strings before handing back to the WSGI server. To do this, you can wrap the result of calling a template function in a call to cinje.stream
.
from cinje import stream
from cinje.std.html import page
def app(environ, start_response):
start_response('200 OK', [("Content-Type": "text/html;charset=utf-8")])
return stream(page(title="Test page."), encoding="utf-8")
If you are using WebOb there are potentially multiple approaches for integrating cinje templates. The recommended method is the one that allows streaming:
from webob import Request, Response
from cinje import stream
from cinje.std.html import page
def app(environ, start_response):
req = Request(environ)
res = Response(content_type='text/html', charset="utf-8")
res.body_iter = stream(page(title="Test page."), encoding=res.charset)
return res(environ, start_response)
Within other frameworks, use can be easier. WebCore 2, for example, understands how to stream encode unicode objects itself, so within your controllers you can simply return the result of calling a cinje template generator directly.