用于模板引擎的扭曲适配器

txTemplate的Python项目详细描述


https://travis-ci.org/steder/txtemplate.svg?branch=master

txtemplate为一些流行的模板引擎提供适配器 使它们在twisted web中易于调用和使用。

txTemplate使用zope.interface提供一致的 每个模板引擎的加载程序和模板接口。

支持的模板引擎

  • ClearSilver
  • Genshi
  • Jinja2

开始

在twisted中使用txtemplate真正需要做的是:
  • 导入它
  • 创建指向模板目录的加载程序
  • 使用所述加载程序加载模板
  • 调用template.render,将回调附加到呈现的延迟(如果需要)并返回twisted.web.resource.resource render方法的延迟。

下面是使用txtemplate的twisted web资源的代码示例 呈现一个可爱的genshi模板,在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()

这个例子包含在txtemplate/examples/txweb.pyroot.xhtml模板位于txtemplate/examples/templates/root.xhtml中。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java StoredProcedureCall 1x Varchar输出1x游标输出   java StackOverflower运行时错误   算法Java基准测试:确保对象在超出范围后不被重用   java在SpringDataNeo4j中使用RelationshipEntity保存节点的正确方法是什么?   命令行参数设置登录java的属性   Java泛型AnyType,如何允许调用任何方法?   java如何检查Html字符串和字符串   sql如何插入一个日期,然后从java执行的存储过程中向oracle数据库中插入的日期添加小时、分钟和秒   java 安卓 3布局相同的活动   安卓应用程序中的java毕达哥拉斯   使用javaw在批处理文件中运行JAR的服务会在process explorer XYNTService中产生多个java进程   java需要在Derby中编写正确的查询   多线程Java在缓存中为多个线程保存变量   持久化java实体引用问题   java在SpringMVC应用程序中使用本地线程安全吗?   JavaSwing,100个文本字段都有类似的任务,所以我想写一个函数来完成这个任务   java我们在新字符串(“literal”)中放置的字符串文字发生了什么变化;   java注入需要在GUI中使用枚举的对象   在Spark SQL中加载JDBC表时java数据不正确