如何更改 Pyramid 中的模板引擎?

发布于 2024-12-16 16:50:14 字数 492 浏览 0 评论 0原文

特别是我想使用 pystache 但其他模板引擎的任何指南都应该足以设置它向上。

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

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

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

尽管我发现 关于如何添加模板引擎的文档,我没有设法设置它。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁的新欢旧爱 2024-12-23 16:50:14

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

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

我做了什么:

创建了文件 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')

工作:)

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

What I did:

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

added this to the __init__.py:

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

working :)

尛丟丟 2024-12-23 16:50:14

add_renderer 的第二个参数应该是一个实现“添加新渲染器”中所示接口的类。 Pyramid 将获取 pystache_renderer_factory 并尝试导入它,因此在您的代码中, import pystache_renderer_factory 行必须起作用。此示例永远不会解析为类,而只会解析为模块或包,因此您必须首先修复它。它应该类似于 mypackage.pystache_renderer_factory。

学习如何编写渲染器的最佳方法可能是查看一些已经编写的渲染器。特别是pyramid_jinja2包,或者在Pyramid的源代码中,有jsonjsonp渲染器的非常简单的实现。请注意它们如何提供相当独特的方法来实现所需的接口。每个工厂接受一个 info 对象,并返回一个接受 valuesystem 对象的可调用对象。

https://github.com/Pylons/pyramid_jinja2/blob/master /pyramid_jinja2/init.py#L260

https://github.com/Pylons/pyramid/blob/master/pyramid /renderers.py#L135

add_renderer's second argument is supposed to be a class that implements the interface shown in "Adding a New Renderer". Pyramid will take pystache_renderer_factory and attempt to import it, so in your code the line import pystache_renderer_factory would have to work. This example won't ever resolve to a class, only a module or package, so you'll have to fix that first. It should be something like mypackage.pystache_renderer_factory.

The best way to learn how to write a renderer is probably to look at some that have been written already. Specifically the pyramid_jinja2 package, or in Pyramid's source there are very simple implementations of json and jsonp renderers. Notice how they all provide fairly unique ways to implement the required interface. Each factory accepts an info object, and returns a callable that accepts value and system objects.

https://github.com/Pylons/pyramid_jinja2/blob/master/pyramid_jinja2/init.py#L260

https://github.com/Pylons/pyramid/blob/master/pyramid/renderers.py#L135

水水月牙 2024-12-23 16:50:14

请注意,这个答案效果很好,直到您使用脚手架创建 Pyramid 项目。一旦你这样做了,这个相关的答案将在构建你的 Pystache/Mustache_Renderer_Factory 时变得更有用:How to将 pystache 与金字塔集成?

Note that this answer works well until you create your Pyramid project with a scaffold. Once you do so, this related answer will prove more useful when constructing your Pystache/Mustache_Renderer_Factory: How to integrate pystache with pyramid?.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文