Cakephp:更改位置模板文件夹

时间:2014-07-05 14:16:19

标签: php cakephp cakephp-2.0

我通过stackoverflow搜索了我的答案,但不幸的是我找不到我想要的答案。

我要做的是通过用户的选择来切换每页的布局。我所做的是数据库中有一列,它们保存了模板的位置。

问题在于cakephp $this->layout自动使用Views/Layouts文件夹。

但要找到模板包,我需要查看项目中的文件夹内容。所以我尝试过使用

App::uses('Folder', 'Utility');
App::uses('File', 'Utility');

这些实用程序的问题是它们只检查webroot中的文件夹。我无法切换到Views/Layouts目录。

所以我的问题是当$this->layout使用目录Views/Layout而实用程序只搜索webroot - 文件夹时,它们永远不会相互联系。

数据库:
|模板|
| TEMPLATEPATH |

Cakephp:

public function getTemplates() {
    $res = $this->getDirectory('Templates');
    if ($res):
        $templatedir = array();
        foreach ($res[0] as $directory):

            $res1 = $this->getDirectory('Templates' . DS . $directory);

            $templatedir[$directory] = $this->_optionTemplates($res1[1]);
        endforeach;

    endif;

    return $templatedir;
}

public function getDirectory($dirname) {
    $Folder = new Folder('/'.$dirname);

    $check = $Folder->inPath(WWW_ROOT . $dirname . DS, true);
    if (!$check):
        return false;
    endif;

    $dir = new Folder(WWW_ROOT . $dirname);
    return $dir->read();
}

private function _optionTemplates($directory) {
    $pattern = array(0 => '/.ctp/');
    if ($directory):
        $templatefiles = array();
        foreach ($directory as $file):
            $template = preg_replace($pattern, ' ', $file);
            $templatefiles[$template] = $template;
        endforeach;
        return $templatefiles;
    endif;
    return false;
}

有人建议我如何解决这个问题吗?

尝试切换$this->layout的位置或切换实用程序的搜索位置都没问题。其他解决方案也欢迎。

PS:抱歉我的英语不好。

0 个答案:

没有答案
相关问题