Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Using layouts/Themes
  • Hi All,

    After using Zend 1.x for a long time, I decided it was time to try a new framework that was a little less heavy. Zend 2.0 didnt impress me much so it was the perfect excuse to learn Fuel!

    I'm having trouble understanding how the presentation layer works with Fuel. With Zend I would write my views as normal, and then create a header.phtml and footer.phtml template file in the layouts section. The view would then be squeezed in between these two templates.

    I took a look at the Theme section of the documentation, and I didnt really understand how this works. I deduced as much that the views are taken out of the views/ directory and into a theme folder. There also seemed to be a lot of code in the controller (the before() and after() methods) in order to use this class; surely that code doesnt have to be duplicated in each of the controllers?

    Ultimately, what I would like to do is create a header/footer for a main layout, and then each module in the application would inherit this template. A customer would then be able to brand the software by just changing the header/footer of the layout.

    I realise there is a lot to take in here, although the whole presentation layer of fuel has got me really confused!

    Cheers
  • There are two ways of dealing with the presentation layer, depending on the complexity you need.

    For normal template based layout, have your controller extend Controller_Template, and define the page template to load in the controller property. This template is a normal view which includes the page template, and placeholders ( in the form of <?php echo $sidebar; ?> ) where your variable data has to come. In your controller you can assign sub-views to these placeholders:

    $this->template->sidebar = \View::forge('mysidebar');

    That's all, and you're in business. Check the docs (http://docs.fuelphp.com/general/views.html, nested views) for some examples.

    You use the Theme class if you have a more complex page layout, as the theme class supports template layering, multiple views per placeholder (like different widgets for the sidebar), and partial chrome support (styling for a placeholder area).

    You also use the theme class if your application needs to support multiple themes, which is the main reason for pulling the views out of the 'code' folders. If you work with modules that are re-used in different applications, you don't want to switch views everytime you use it somewhere. That will quickly become unmanageble. Instead, use default 'theme' views, and allow a theme to override them to provide a different look and feel, based on the app the module is used in.
  • If one were using Controller_Template inside a module, would it be possible to inherit the main application's template.php, if so, how would you do that?
  • One simple solution is to create a Controller_Base (a base controller) that extends the Controller_Template and then, in your module, extends that controller.

    You can put there every configuration you want and it is automatically present in your module's controller.
  • template.php (assuming it's a view) will just be loaded.

    If a view is loading within a module context (i.e. the module controller is loaded by a Request) it will be loaded from the modules' views folder. And if not exist, the app and package folders will be searched.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion