如何在Pyramid中更改模板引擎? [英] How to change the template engine in Pyramid?

查看:79
本文介绍了如何在Pyramid中更改模板引擎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

特别是我想使用 pystache ,但是任何其他模板引擎指南都应该足够好进行设置.

In particular I want to use pystache but any guide for another template engine should be good enough to set it up.

如果我理解正确,则必须在我的金字塔应用程序的__init__.py中注册渲染器工厂.

If I understood correctly, I have to register the renderer factory in the __init__.py of my pyramid application.

config = Configurator(settings=settings)
config.add_renderer(None, 'pystache_renderer_factory')

现在我需要创建渲染器工厂,不知道如何.

Now I need to create the renderer factory and don't know how.

即使我找到了

Even though I found the documentation about how to add a template engine, I didn't manage to set it up.

推荐答案

最后,我能够按照该指南添加pystache模板引擎:

Finally I was able to add the pystache template engine following this guide:

https://groups.google.com /forum/#!searchin/pylons-discuss/add_renderer/pylons-discuss/Y4MoKwWKiUA/cyqldA-vHjkJ

我做了什么:

创建了文件mustacherenderer.py:

created the file mustacherenderer.py:

from pyramid.asset import abspath_from_asset_spec 
import pystache
import os 

def pystache_renderer_factory(info):
    template = os.path.join(abspath_from_asset_spec('myproj:templates', False),
                            info.name)
    f = open(template) 
    s = f.read() 
    f.close() 
    def _render(value, system):
        return pystache.render(s, value)
    return _render

将此添加到__init__.py:

config.add_renderer('.pmt', 'myproj.mustacherenderer.pystache_renderer_factory')

工作:)

这篇关于如何在Pyramid中更改模板引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆