Jump to content

Theme system for Symfony


Recommended Posts

So I started using Symfony a few weeks back, figured I should probably learn one of these frameworks rather than always do things custom. So far I like it, but it's been a bit of a learning curve from how I've typically done things.


One thing I'd like to do with my project is setup a system for using themes on the site, but I've not yet figured out how to do so in a manner that I like. I'm hoping maybe someone with more Symfony experience can shed some light on how best to approach this. I'll give a quick rundown of how I think I'd like for things to work. Any suggestions are welcome.


What I'd like to be able to do is set a path to a directory which contains all the themes. Installing a new theme should be as simple as just extracting a zip file into this directory. The active theme would be controlled through a configuration parameter. So essentially when someone sets up the software, they would just add a couple lines to their parameters.yml file, for example:

    themes_root: %kernel.root_dir%/../web/themes
    active_theme: default
A theme would consist of a few twig templates for various types of pages (or snippets). The controller views would extend these templates as necessary. The theme's templates should be able to extend other templates within that theme as well. For example a theme may have a base template and various pages would extend that.


My initial thought for how to do all of this was to create a twig extension to add a function which would resolve the path to a theme template. For example my view would do:

{% extends ThemePath("simplepage.html.twig") %}
{% block Content %}
{% endblock %}
The ThemePath would use the parameters to construct the full path to the twig template, for example D:\site\web\themes\default\simplepage.html.twig. I completed this part but ran into my a snag with Symfony complaining that the template name passed to extends was not in bundle:section:template.format.engine format.


I managed to work around this by replacing the default name parser to allow absolute file paths or the Symfony format. This is working all well and fine but there are a few things I don't like about this setup:

  • It feels ugly to me. Seems like there should be a better way than this.
  • The theme files need to use the ThemePath function as well if they use extends or similar. Ideally this would be unnecessary.
Any comments/thoughts/suggestions/questions are welcome.
Link to comment
Share on other sites

In Symfony it is possible for one bundle to override another bundle. This allows another bundle to override any page as necessary and thus theme it however you like. The only problem is you can only override one bundle not multiple. Using this setup you can create a bundle for every theme and simply:


php composer.phar require me/bundle/grey-theme-bundle
The advantage here is that your bundle with it's controllers and views is usable without any dependency on another bundle which it would have if your views extended some other bundles base views.


It's even possible to install multiple theme bundles and then afterwards select which theme bundle to activate. The added benefit is that it will also be under it's own web bundle directory when executing assets:install:








Edited by ignace
Link to comment
Share on other sites

Thanks, I had seen some of the override ability previously, though not that particular page. It seemed like a bit much to have to create and register a bundle just to change themes though, and didn't seem to fit well with what I had thought. I may re-visit the idea however.


I'll keep plugging away at a few ideas and post back with whatever I decide to go with later.

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.