2

I use the CakePHP framework and I want to have multiple templates in my project.
Is there any way for implementation of multiple templates in CakePHP?

For example, an admin can choose first the or second template in the backend and users can use the same template. (Like in the Joomla backend). If there is any way, how can I implement this?

2 Answers 2

5

Just giving you basic idea about how you can do that.

In app_controller try below code.

<?php
class AppController extends Controller
{
    var $components = array( 'Auth','Session', 'RequestHandler','Email','Gzip.Gzip','SwiftMailer');

    var $helpers = array( 'Javascript', 'Form', 'Html', 'Session','Time','Custom','Paginator','Text' );

    function beforeFilter()
    {
        if(isset($this->params['admin']) && $this->params['admin'] == 1)
        {
            $this->layout = "admin";
        }
        else
        {
            $this->layout = "default";
        }       
    }
?>

And inside other controller file which extends app_controller add you must have code as below.

<?php
class OtherController extends Controller
{
    var public $uses = array('ModelName');

    function beforeFilter()
    {
        parent::beforeFilter();
    }
?>

You can also overwrite $this->layout to every controller action.

0
1

You can create different templates in View/layouts

template_1.ctp, template_2.ctp with different styles

And create the default.ctp layout which will include one of the existing templates or set $this->layout = 'template_1'; in the AppController;

<?php
    //default.ctp
    $loadTemplate = 'template_1.ctp';//value from database or config file?
    include_once($loadTemplate);
?>

Or you could use themes as per documentation

1
  • include / require should not be used for loading templates.
    – dogmatic69
    Jan 23, 2013 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.