1

I want to make some of my templates user editable inside my web app (the actual template, not just replacing content via replacement variables). 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. But is there a way to get the actual file location with the some.package:templates/template.pt syntax?

I want to get a list of template files in, let's say some.package:templates/email/. Is there a way of doing that?

Falmarri
  • 47,727
  • 41
  • 151
  • 191

1 Answers1

0

To go from a dotted asset specification you may use pyramid.path.AssetResolver, as the documentation states, it looks like this:

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 (e.g. /path/to/myproject/templates/foo.pt) or an absolute asset spec (e.g. myproject:templates/foo.pt), an asset descriptor is returned without taking into account the package passed to this class' constructor.


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. At that point you can use the standard Python open/close file methods to have the user update the templates.

X-Istence
  • 16,324
  • 6
  • 57
  • 74