CakePHP 主题插件视图

问题描述 投票:0回答:2

是否可以为插件视图设置主题?我有一个适用于移动设备的移动主题,并且也想为插件应用程序/视图使用不同的视图文件。我尝试过 app/views/themed/THEME/plugin/... 和 /app/plugins/PLUGIN/views/themed/THEME/... 似乎都不起作用。预先感谢。

php cakephp cakephp-1.3
2个回答
5
投票

将主题内容复制到: 应用程序/视图/主题/主题名称/插件/插件名称/app/libs/view/theme_plugins.php 上创建一个 ThemePluginsView

// app/libs/view/theme_plugins.php
if (!class_exists('ThemeView'))
    App::import('Core', 'view/theme');

class ThemePluginsView extends ThemeView {

    function __construct(&$controller, $register = true) {
        parent::__construct($controller, $register);
    }

    function _paths($plugin = null, $cached = true) {
        $paths = parent::_paths($plugin, $cached);
        if (!is_string($plugin) || empty($plugin) || empty($this->theme))
            return $paths;
        // add app/plugins/PLUGIN/views/themed/THEME path 
        $dirPath = APP . 'plugins' . DS . $plugin . DS . 'views' . DS . 'themed' . DS . $this->theme . DS;
        if (is_dir($dirPath))
            $paths = array_merge(array($dirPath), $paths);
        return $paths;
    }
}

然后在 app_controller 上的 beforeFilter() 或普通控制器上的 beforeFilter() 上调用它,如下所示:

function beforeFilter() {
  if (!class_exists('ThemePluginsView'))
    App::import('Core', 'view/theme_plugins');
  $this->view = 'ThemePlugins';
  $this->theme = 'THEME_NAME';
}

希望有帮助


4
投票

Cakephp 2.x 支持此功能,无需更改任何代码:

如果您(可以)转换为使用 cakephp 2.x,那么您可以(自动)。主题“mytheme”和插件“Permissions”的视图路径为:

Array
(
    [0] => /var/vhosts/Project/htdocs/app/View/Themed/Mytheme/Plugin/Permissions/
    [1] => /var/vhosts/Project/htdocs/app/View/Themed/Mytheme/
    [2] => /var/vhosts/Project/htdocs/app/View/Plugin/Permissions/
    [3] => /var/vhosts/Project/htdocs/app/Plugin/Permissions/View/
    [4] => /var/vhosts/Project/htdocs/app/View/
    [5] => /var/vhosts/Project/htdocs/lib/Cake/View/
    [6] => /var/vhosts/Project/htdocs/lib/Cake/Console/Templates/skel/View/
)

因此,如果您的插件中有 Users/index.ctp 并且想要覆盖它,您可以编辑:

/var/vhosts/Project/htdocs/app/View/Themed/Mytheme/Plugin/Permissions/Users/index.ctp

对于主题版本或:

/var/vhosts/Project/htdocs/app/View/Plugin/Permissions/Users/index.ctp

对于非主题版本

© www.soinside.com 2019 - 2024. All rights reserved.