txTemplate
txTemplate provides adapters for a few popular template engines to make them easily callable and usable within Twisted Web.
txTemplate uses zope.interface to provide a consistent loader and template interface for every template engine.
Supported Template Engines
ClearSilver
Genshi
Jinja2
Getting Started
- All you really need to do to use txTemplate in twisted is:
import it
create a loader pointed at your template directory
load a template with said loader
call template.render, attach callbacks to the rendered deferred (if desired) and return the deferred from and of your twisted.web.resource.Resource render_* methods.
Here’s a code sample of a Twisted Web resource that uses txTemplate to render a lovely Genshi template to say Hello World in HTML:
import os
from twisted.application import internet
from twisted.internet import reactor
from twisted.web import resource
from twisted.web import server
import txtemplate
TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"templates")
class HelloWorld(resource.Resource):
def __init__(self):
resource.Resource.__init__(self)
self.loader = txtemplate.GenshiTemplateLoader(TEMPLATE_DIR)
def getChild(self, name, request):
return self
def render_GET(self, request):
template_name = "hello.xhtml"
template = self.loader.load(template_name)
context = {"greeting": "Hello",
"greetee": "World"}
def cb(content):
request.write(content)
request.setResponseCode(200)
request.finish()
d = template.render(**context)
d.addCallback(cb)
return server.NOT_DONE_YET
site = server.Site(HelloWorld())
reactor.listenTCP(8888, site)
reactor.run()
This example is included in txtemplate/examples/txweb.py and the root.xhtml template is in txtemplate/examples/templates/root.xhtml.
Metadata
Release files for txTemplate 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| txTemplate-1.0.4.tar.gz | 9.8 kB | Without provenance | Details for txTemplate-1.0.4.tar.gz |
Release files / txTemplate-1.0.4.tar.gz
| Download URL | txTemplate-1.0.4.tar.gz |
|---|---|
| Size | 9.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ed90df6cde0c832b6dce4c82aacd1811bc0a5ef25e086de3d577a19f9871607b
|
|
BLAKE2b-256 checksum How to use checksums |
d2634c0e25c903358279eba9d6abef7902ecfb92429a11ac2eda40e7a458a23e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for txTemplate 1.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.