Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "view theme"

Support for Multiple Themes / Styles in Symfony

This will allow you to use multiple themes for symfony, each with it's own templates and stylesheets.

Download

Grab the code at GitHub: https://github.com/acetous/Symfony-Themed

New Directory for Templates

The templates have to be stored in sf_root_dir/themes/application/themename/module. A theme named default has to be present. Layouts have to be placed in sf_root_dir/themes/themename/application.

Example:

Stylesheets

Create stylesheets for your theme. You'll need to create one for each application.

Example:

Please include this stylesheets in your themes root directory. They need to be copied into the /web/css folder.

Only Change What You Need

All themes (except default) will load the default-templates if a custom one is not found. This way you just need to create the template files you want to alter.

Apart from that the default-stylesheet will be included if a custom one cannot be found.

Installation

Setup a Theme

Just run the ./symfony themed:setup task to setup all themes or ./symfony themed:setup name to setup a specific theme.

by acetous on 2011-07-09, tagged style  template  theme  view 
(3 comments)

One template, multiple themes

Some applications want to offer various look-and-feels for each page. The online documentation already explains how to change the CSS from the server, and a bit of JavaScript will allow you to do it from the client side.

But sometimes this is not enough, and the need arises to have several sets of templates for each action. Something like:

mymodule/
  templates/
    indexSuccess.php    # Template for index action, default theme
    theme1/
      indexSuccess.php  # Template for index action, theme1
    theme2/
      indexSuccess.php  # Template for index action, theme2

Thanks to the MVC architecture, this is very easy to do.

We'll suppose that theme is an attribute of the sfUser object. It contains a simple string. The way to determine the preferred theme for a given user will be left to your sagacity, as well as the way to extend the sfUser class to deliver this attribute. The interesting thing is to modify the view class to have it use the theme when the time comes to look for templates and layouts.

Create a myView.class.php file in the lib/ directory of your application, and write in:

<?php
 
class myView extends sfPHPView
{
  public function configure()
  {
    parent::configure();
 
    // Grab the theme from the user (of from anywhere else)
    $theme = $this->getContext()->getUser()->getTheme();
 
    // If there is a theme and if the theme feature is enabled
    if($theme && sfConfig::get('app_theme'))
    {
      // Look for templates in a $theme/ subdirectory of the usual template location
      if (is_readable($this->getDirectory().'/'.$theme.'/'.$this->getTemplate()))
      {
        $this->setDirectory($this->getDirectory().'/'.$theme);
      }
 
      // Look for a layout in a $theme/ subdirectory of the usual layout location
      if (is_readable($this->getDecoratorDirectory().'/'.$theme.'/'.$this->getDecoratorTemplate()))
      {
        $this->setDecoratorDirectory($this->getDecoratorDirectory().'/'.$theme);
      }
    }
  }
 
}

To force symfony to use this view class instead of the default sfPHPView, create a module.yml in the application's config/ directory, and write in it:

all:
  view_class: my

The new view will look for themes only if you enabled the feature in your app.yml:

all:
  theme: on

Clear the cache, and the theme feature is ready.

It works like this: when a user has a defined theme, symfony will look for templates and layouts in a subdirectory named by this theme. For instance, if a user has a foobar theme, and that it requests the mymodule/myaction action, symfony will look for the template in:

apps/myapp/modules/mymodule/templates/foobar/myActionSuccess.php

and for the layout in:

apps/myapp/templates/foobar/layout.php

The beauty of this modification is that if the themed template doesn't exist, symfony will use the normal template as a fallback.

It must be pretty easy to package this into a plugin, so if you feel like doing it...

by Francois Zaninotto on 2006-11-21, tagged template  theme  view 
(7 comments)