How to use a symfony 1 layout from a different directory

Warning! This post is either deprecated or outdated.

In symfony 1 it is possible to have different layouts for an application. But they all have to be put into the directory ‘myproject/apps/frontend/templates/’. But what if you want to use a layout from another location?

Assume you make a plugin with a specific layout, it would be nice to load the layout from the plugin directory, and not to have to copy the file to the global directory.

Here is how you can achieve this:

$template = $this->getContext()->getConfiguration()->getTemplateDir('MODULE', 'LAYOUT_FILE.php');
$this->setLayout($template . '/LAYOUT_FILE');

Let’s say you have the following:

my_project/
  plugins/
    my_plugin/
      modules/
        MyUser/
          actions/
            actions.php
          templates/
            indexSuccess.php
            MyUserLayout.php

The actions.php class could be something like this:

class MyUserAction extends sfActions {

  public function preExecute() {
    $template = $this->getContext()->getConfiguration()->getTemplateDir('MyUser', 'MyUserLayout.php');

    $this->setLayout($template . '/MyUserLayout');
  }

  public function executeIndex() {

  }
}

Have fun!

By the way, if you found a typo, please fork and edit this post. Thank you so much! This post is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Comments

Fork me on GitHub