如何更改 Pyramid 中的模板引擎?
特别是我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后,我能够按照本指南添加 pystache 模板引擎:
https://groups.google.com/forum/#!searchin/pylons-discuss/add_renderer/pylons-discuss/Y4MoKwWKiUA/cyqldA-vHjkJ
我做了什么:
创建了文件 Mustacherenderer.py:
将其添加到
__init__.py
:工作:)
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:
added this to the
__init__.py
:working :)
add_renderer
的第二个参数应该是一个实现“添加新渲染器”中所示接口的类。 Pyramid 将获取 pystache_renderer_factory 并尝试导入它,因此在您的代码中, import pystache_renderer_factory 行必须起作用。此示例永远不会解析为类,而只会解析为模块或包,因此您必须首先修复它。它应该类似于 mypackage.pystache_renderer_factory。学习如何编写渲染器的最佳方法可能是查看一些已经编写的渲染器。特别是
pyramid_jinja2
包,或者在Pyramid的源代码中,有json
和jsonp
渲染器的非常简单的实现。请注意它们如何提供相当独特的方法来实现所需的接口。每个工厂接受一个info
对象,并返回一个接受value
和system
对象的可调用对象。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 takepystache_renderer_factory
and attempt to import it, so in your code the lineimport 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 likemypackage.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 ofjson
andjsonp
renderers. Notice how they all provide fairly unique ways to implement the required interface. Each factory accepts aninfo
object, and returns a callable that acceptsvalue
andsystem
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
请注意,这个答案效果很好,直到您使用脚手架创建 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?.