简体   繁体   English

从视图中列出金字塔模板文件

[英]Listing pyramid template files from a view

I want to make some of my templates user editable inside my web app (the actual template, not just replacing content via replacement variables). 我想让我的某些模板用户在我的Web应用程序中可编辑(实际模板,而不仅仅是通过替换变量替换内容)。 But I can't figure out how to get a list of template files. 但是我不知道如何获取模板文件列表。

Obviously if I know the real path I can just us regular python tools. 显然,如果我知道真实的路径,就可以使用常规的python工具。 But is there a way to get the actual file location with the some.package:templates/template.pt syntax? 但是有没有一种方法可以使用some.package:templates/template.pt语法来获取实际的文件位置?

I want to get a list of template files in, let's say some.package:templates/email/ . 我想获取模板文件列表,比如some.package:templates/email/ Is there a way of doing that? 有办法吗?

To go from a dotted asset specification you may use pyramid.path.AssetResolver , as the documentation states, it looks like this: 文档所述,要使用点状资产规范,可以使用pyramid.path.AssetResolver ,如下所示:

from pyramid.path import AssetResolver

a = AssetResolver()
resolver = a.resolve('myproject:templates/foo.pt')
print(resolver.abspath())
# -> /path/to/myproject/templates/foo.pt

Specifically it has this to say about resolving paths: 具体来说,它有关于解决路径的说法:

If spec is an absolute filename (eg /path/to/myproject/templates/foo.pt ) or an absolute asset spec (eg myproject:templates/foo.pt ), an asset descriptor is returned without taking into account the package passed to this class' constructor. 如果spec是绝对文件名(例如/path/to/myproject/templates/foo.pt )或绝对资产规范(例如myproject:templates/foo.pt ),则返回资产描述符,而不考虑传递给的包此类的构造函数。


However I would highly recommend against letting users modify the template files inside your package directly. 但是,我强烈建议您不要让用户直接修改包中的模板文件。 What I would recommend instead is that you in your installation procedures have a way of copying all your templates out of your package, and then using Pyramids asset override mechanism to sub out the existing asset specs with a new location. 相反,我建议您在安装过程中使用一种方法,将所有模板从软件包中复制出来,然后使用Pyramids资产替代机制用新的位置对现有资产规格进行分类。 At that point you can use the standard Python open/close file methods to have the user update the templates. 此时,您可以使用标准的Python打开/关闭文件方法来让用户更新模板。

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

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