Very Simple Theming in Pylons

Posted on January 5th, 2010 by raoul and tagged , , , .

For a while I've been thinking about how to achieve themes in some of my Pylons applications, and just today I was looking around in my environment.py file, and saw the configuration of the templates and public directories. That gave me an idea, can I load a theme directory from the configuration file and use it in there?

I had a bit of a false start initially, I thought that I could just use the config object as I do elsewhere in my application, but that didn't want to work. Eventually I found the solution: the app_conf object.

This is how my code now looks:

    ...
    theme_dir = os.path.join(app_conf['themes.directory'], app_conf['themes.current'])
    paths = dict(root=root,
                 controllers=os.path.join(root, 'controllers'),
                 static_files=os.path.join(theme_dir, 'public'),
                 templates=[os.path.join(theme_dir, 'templates')]
    )
    ...

And the relevant lines in my config.ini file:

themes.directory = %(here)s/themes
themes.current = default

Each theme is simply a directory with two subdirectories, "public" where my images, styles and scripts live, and "templates" where my template files live.

I'm still looking into how I can pull the current theme from the database at this early stage of the application, but this is a start in the right direction.

Hopefully, this will help someone else. If you have any suggestions for a better or more elegant solution, I'd be keen to hear about it.