简体   繁体   English

如何将pystache与金字塔集成?

[英]How to integrate pystache with pyramid?

I would like to use the class based views that pystache offers in my pyramid application, but I'm not entirely sure how to integrate the two properly. 我想使用pystache在我的金字塔应用程序中提供的基于类的视图,但我不完全确定如何正确地集成这两个视图。 I've read this already, but it doesn't talk about using the class based views. 我已经读过这篇文章 ,但它没有讨论使用基于类的视图。

How would I create a new renderer for pystache if I wanted to use class based views? 如果我想使用基于类的视图,我如何为pystache创建一个新的渲染器? Can somebody help me out here? 有人可以帮帮我吗?

Also, while I already know how mustache works, I can't seem to find much information on the python implementation (pystache). 此外,虽然我已经知道胡子如何工作,但我似乎无法找到有关python实现(pystache)的更多信息。 Can somebody point me in the right direction here? 有人能指出我在正确的方向吗?

Implement a MustacheRendererFactory : 实现一个MustacheRendererFactory

class MustacheRendererFactory(object):
  def __init__(self, info):
    self.info = info

  def __call__(self, value, system):
    package, filename = resolve_asset_spec(self.info.name)
    template = os.path.join(package_path(self.info.package), filename)
    template_fh = open(template)
    template_stream = template_fh.read()
    template_fh.close()
    return pystache.render(template_stream, value)

Update your configurator setup, probably in __init__.py : 更新配置程序设置,可能在__init__.py

def main(global_config, **settings):
  config = Configurator(settings=settings)
  # ...
  # Use Mustache renderer
  config.add_renderer(name='.mustache',
    factory='myapp.mustacherenderer.MustacheRendererFactory')
  # ...

Use in your views: 在您的视图中使用:

@view_config(route_name='myview', renderer='myapp:templates/notes.mustache')
def my_view(request):
  # ...

In pyramid, the renderer view argument is a string, it can't be a class. 在金字塔中,渲染器视图参数是一个字符串,它不能是一个类。 Thus, there is no way to just say 因此,没有办法说

@view_config(route_name='someroute', renderer=MyClassBasedView)

The easiest solution might be to call the renderer manually. 最简单的解决方案可能是手动调用渲染器。

return Response(pystache.render(ViewClass))

If you really want to use the pyramid renderer system, you can use a fake renderer string of the form "dotted path to class + extension". 如果您确实想使用金字塔渲染器系统,则可以使用“dot + extension”的虚拟渲染器字符串。 The renderer factory would then resolve the dotted path to get the class and return the renderer. 然后,渲染器工厂将解析虚线路径以获取类并返回渲染器。

I must say I'm not sure I understand how you would use pystache class based views in pyramid. 我必须说我不知道​​你如何理解如何在金字塔中使用基于pystache类的视图。 Defining class with methods that return values seems more complicated than returning a dict, and computing the values in those methods instead of doing it in the pyramid views might lead to more cluttered code. 使用返回值的方法定义类似乎比返回dict更复杂,并且计算这些方法中的值而不是在金字塔视图中执行它可能会导致更混乱的代码。 The inheritance might have some advantages I haven't considered, though. 但是,继承可能有一些我没有考虑过的优点。


As for pystache, the documentation seems limited to the pypi page , but the code is clean and easy to read (I skimmed through it before answering the question). 至于pystache,文档似乎仅限于pypi页面 ,但代码干净且易于阅读(我在回答问题之前浏览了它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM