Cascading pylons.paths: templates vs static files

36 views
Skip to first unread message

Yannick Gingras

unread,
Oct 8, 2007, 2:58:18 PM10/8/07
to pylons-...@googlegroups.com

Greetings Plyloneers,
I'm hacking a little community engine and I'd like it to be easily
skinnable. Mako, with its template inheritance, makes it easy but
there are still a few hacks to be done with the path resolution.
Basically, what I'd like is that a site using the community engine
would install it and customize its look without messing with the
packages source files. Something like:

$ easy_install theengine
$ paster make-config theengine test.ini
$ paster setup-app test.ini
$ emacs extra_templates/base.mako
$ emacs extra_files/base.css
$ paster serve test.ini

For the templates part, this is really easy. In environment.py, one
can do:

paths = dict(root=root,
controllers=os.path.join(root, 'controllers'),
static_files=os.path.join(root, 'public'),
templates=[os.path.join(global_conf["here"],
"extra_templates"),
os.path.join(root, 'templates'),])

and `render("foo.mako")` will look for the files in roughly this
order:

%(here)s/extra_template/
/path/to/the/egg/theengine/templates/

The cascading lookup is not only for `foo.mako`, it's for all included
or inherited files. This is perfect.

First question: is it possible to include directories in the templates
path list from the .ini without doing the manual parsing in
environment.py?

Second question: the cascading templates path list is perfect and I'd
like to do the same with the static files; the css and images could
easily be changed by the user. Unfortunately, passing a list to
`static_files` won't work. Is there a way to have cascading static
files? There must be one because Pylons supply it's own images and
css for the error pages.

--
Yannick Gingras

Yannick Gingras

unread,
Oct 8, 2007, 3:50:01 PM10/8/07
to pylons-...@googlegroups.com
Yannick Gingras <ygin...@ygingras.net> writes:

> Unfortunately, passing a list to `static_files` won't work. Is
> there a way to have cascading static files?

Thanks to Chairos on #pylons, on just have to hack middleware.py:

static_apps = [StaticURLParser(path)
for path in config['pylons.paths']['static_files']]
app = Cascade(static_apps + [javascripts_app, app])

--
Yannick Gingras

Ches Martin

unread,
Oct 10, 2007, 2:25:04 PM10/10/07
to pylons-discuss
Thanks for sharing this stuff, Yannick. One of those things I knew I
was going to need at some point. It *would* be nice if some of this
was doable from the .ini. At any rate, sounds like a good start for a
cookbook entry on a "skinning" setup :-)

--
Ches Martin


On Oct 8, 3:50 pm, Yannick Gingras <yging...@ygingras.net> wrote:

Yannick Gingras

unread,
Oct 10, 2007, 7:45:43 PM10/10/07
to pylons-...@googlegroups.com
Ches Martin <ches....@gmail.com> writes:
>> [how I do Cascading static files]

> Thanks for sharing this stuff, Yannick. One of those things I knew I
> was going to need at some point. It *would* be nice if some of this
> was doable from the .ini. At any rate, sounds like a good start for a
> cookbook entry on a "skinning" setup :-)

Well, once you reach that point it's trivial to add .ini lookup. My
.ini looks like:

[app:main]
extra_templates = %(here)s/extra_templates
extra_statics = %(here)s/extra_statics
...

environment.py has

paths = dict(root=root,
controllers=os.path.join(root, 'controllers'),

static_files=[app_conf["extra_statics"],
os.path.join(root, 'public')],
templates=[app_conf["extra_templates"],
os.path.join(root, 'templates'),])

and middleware.py has

# Static files
javascripts_app = StaticJavascripts()


static_apps = [StaticURLParser(path)
for path in config['pylons.paths']['static_files']]
app = Cascade(static_apps + [javascripts_app, app])

return app

--
Yannick Gingras

Reply all
Reply to author
Forward
0 new messages