我如何在cakephp中实现多模板 [英] How can i implement multi template in cakephp

查看:84
本文介绍了我如何在cakephp中实现多模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CakePHP框架,并且希望在我的项目中有多个模板。

是否可以在CakePHP中实现多个模板?



例如,管理员可以在后端选择第一个或第二个模板,而用户可以使用相同的模板。 (就像在Joomla后端一样)。

解决方案

只要有办法,就可以做到这一点。 / p>

在app_controller中尝试以下代码。

 <?php 
类AppController扩展了控制器
{
var $ components = array('Auth','Session','RequestHandler','Email','Gzip.Gzip','SwiftMailer');

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

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

以及其他扩展app_controller的控制器文件中添加您必须具有以下代码。

 <?php 
类OtherController扩展了控制器
{
var public $ uses = array('ModelName');

函数beforeFilter()
{
parent :: beforeFilter();
}
?>

您还可以覆盖 $ this-> layout 到每个控制器操作。


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?

解决方案

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.

这篇关于我如何在cakephp中实现多模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆