如何将pystache与金字塔集成?

2024-01-22

我想使用 pystache 在我的金字塔应用程序中提供的基于类的视图,但我不完全确定如何正确集成两者。我读了this https://stackoverflow.com/questions/8169606/how-to-change-the-template-engine-in-pyramid已经,但它没有讨论使用基于类的视图。

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

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


实施一个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:

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):
  # ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将pystache与金字塔集成? 的相关文章

随机推荐