如何将pystache与pyramid整合? [英] How to integrate pystache with pyramid?

查看:78
本文介绍了如何将pystache与pyramid整合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用pystache在金字塔应用程序中提供的基于类的视图,但是我不确定如何正确地将两者集成在一起.我已经读过,但是还没有谈论使用基于类的视图.

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. I've read this already, but it doesn't talk about using the class based views.

如果我想使用基于类的视图,如何为pystache创建一个新的渲染器?有人可以帮我吗?

How would I create a new renderer for pystache if I wanted to use class based views? Can somebody help me out here?

另外,虽然我已经知道了胡子的工作原理,但是我似乎找不到关于python实现(pystache)的太多信息.有人可以在这里指出正确的方向吗?

Also, while I already know how mustache works, I can't seem to find much information on the python implementation (pystache). Can somebody point me in the right direction here?

推荐答案

实现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)

可能在__init__.py中更新配置程序设置:

Update your configurator setup, probably in __init__.py:

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

在您的视图中使用:

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

这篇关于如何将pystache与pyramid整合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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