CSS, images and JavaScript in /template?

11 views
Skip to first unread message

jon

unread,
Nov 24, 2009, 1:30:06 PM11/24/09
to pylons-discuss
Hi,

I'm wondering if it's possible to put CSS, images and JavaScript into
the /template directory of Pylons and somehow get the Mako templates
to refer to those files and not the ones in /public?

I'm building a system which is "themeable" and I want to keep all of
the theme files together and figured a structure like this would work
best:

/templates/themes/mytheme
/css
/js
/img
base.mako
layout1.mako
layout2.mako

Thoughts on this? Is there a better way?

Thanks,

Jon

Wyatt Baldwin

unread,
Nov 24, 2009, 1:55:58 PM11/24/09
to pylons-discuss
Maybe put the static files in a subdirectory so that you can change
the config value that says where Pylons should look for static files?

/templates/themes/mytheme
/static/css
/js
/img
base.mako
layout1.mako
layout2.mako

In your middleware, there should be some lines like this:

public_dir = config['pylons.paths']['static_files']
static_app = StaticURLParser(public_dir, cache_max_age=43200)

Instead of getting the public_dir value from 'pylons.paths', which is
hardcoded in environment.py, you could set it in a config file.

One thing I'm curious about is whether it is possible to dynamically
change the StaticURLParser's public_dir location, or would you have to
restart the app?

Jonathan Vanasco

unread,
Nov 24, 2009, 3:29:41 PM11/24/09
to pylons-discuss
you could put everything into

/templates/_themes

and then have routes/a controller dispatch stuff to the right files as
needed

jon

unread,
Nov 24, 2009, 6:22:31 PM11/24/09
to pylons-discuss
These all seem like really good ideas, for certain. What I ended up
doing is in my middleware.py I query my database to get all the
current themes and a dirname attribute. I then build and add the paths
for each theme along with building a StaticURLParser for each theme.
All of that gets passed into Cascade and I'm good to go. Works nicely.

Thanks for the responses!

Wyatt Baldwin

unread,
Nov 24, 2009, 7:21:17 PM11/24/09
to pylons-discuss
On Nov 24, 3:22 pm, jon <jon.wyna...@lucasfilm.com> wrote:
> These all seem like really good ideas, for certain. What I ended up
> doing is in my middleware.py I query my database to get all the
> current themes and a dirname attribute. I then build and add the paths
> for each theme along with building a StaticURLParser for each theme.
> All of that gets passed into Cascade and I'm good to go. Works nicely.

Seems like adding a StaticURLParser for every theme would be a little
inefficient, though perhaps insignificantly so for your app. Do you
actually have a need to have all the themes loaded all the time?

I have a vague idea about using a url_for_theme helper instead, though
this idea would work a little better (I think) if you were willing to
put all of the static files for all themes in the same place. I.e., if
you put all the themes under public/themes, then you could switch
themes simply with url_for_theme(current_theme_name), perhaps storing
current_theme_name in the session, a cookie, or the user's profile.

This doesn't work with your idea of keeping everything in the same
directory, but I wouldn't see that as too big of a deal--a theme could
be "installed" simple by copying the static files for the theme into
public/themes/{theme_name} and the templates to templates/
{theme_name}.

Something else that's good about this approach is that it would more
easily (I think) allow you to switch to serving your themes' static
files directly from Apache.

Anyway, that might not work for your situation, but it seems like an
interesting approach.

jon

unread,
Dec 2, 2009, 5:11:06 PM12/2/09
to pylons-discuss
Yeah, turns out the StaticURLParser didn't work so well. Each of my
themes had different images but with the same name and so I would get
the wrong images in a site, even though it was named correctly. Based
on your suggestion I've switched things up to do a couple things:

1. There's a site object which has theme info. The controller has
access to this and can get the appropriate template.

2. That same object is available to each Mako template and the static
file URLs are dynamic as well, getting the appropriate file for that
theme.

This works great and I don't really mind have two locations for files.

Thanks for the tip!

Cheers,

Jon

Jonathan Vanasco

unread,
Dec 2, 2009, 5:32:16 PM12/2/09
to pylons-discuss
i just thought of something...

what if you have:

pylonsapp/themes/## themename ##

which has 2 sub folders:
/templates
/static

you can them just symlink these themes into the right spots in /public
and /template ; even have a quick py/shell script to automate that

it would let you handle/package all the themes in one place , but
pylons would find them in multiple places.
Reply all
Reply to author
Forward
0 new messages