CodeIgniter & Smarty -无法更改模板目录-桥接智能类不会覆盖setTemplateDir()

a9wyjsp7  于 2022-12-16  发布在  其他
关注(0)|答案(2)|浏览(87)

我试图添加Smarty到CodeIgniter,这里是我做的步骤:
1.下载的CI
1.下载Smarty并将其内容放入“应用程序/第三方/smarty”文件夹
1.已在“应用程序/库”中创建“smarty.php”文件
1.在“应用程序/视图”文件夹中创建了“模板”和“模板_c”文件夹
1.在“application/views/templates”文件夹中创建了简单的“test.tpl”文件
1.打开了“application/config”文件夹中的“autoload.php”并添加了:
$autoload ['库'] =数组('智能');
1.在控制器内部写入$this->smarty->display('test.tpl';
我得到了这个错误-Message: Unable to load template file 'test.tpl'
经过一些调试后,我注意到我的Smarty模板目录被设置为默认目录,而不是桥接类(“application/libraries/smarty.php”)中指定的目录。
下面是桥接类的内容:

<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once(APPPATH.'third_party/smarty/Smarty.class.php');

class CI_Smarty extends Smarty {

    function __construct()
    {
        parent::__construct();
        // NOT Changig this for some reason
        $this->setTemplateDir(APPPATH.'views/templates/');   
        $this->setCompileDir(APPPATH.'views/templates_c/');
    }

}

我认为CI_Smarty类由于某种原因没有执行,这就是为什么我的模板目录被设置为default
还要注意的是,如果我直接进入Smarty.class.php并手动更改模板目录,一切都会正常
解决了这个问题-桥接类应该在“系统/库”中,而不是在“应用程序/库”文件夹中。

2ekbmq32

2ekbmq321#

问题是您需要加载扩展smartyci_smarty库,而不是直接加载smarty

$autoload['libraries'] = array('ci_smarty');

在控制器中,

$this->ci_smarty->display('test.tpl');

重要

请注意,所有本机CodeIgniter库都以CI_为前缀,因此不要将其用作前缀。
请尝试将库名称改为custom_smarty
最后但并非最不重要

无论任何原因都不要触及系统目录,永远不要。这不是最佳做法。您可以轻松地创建新库或扩展application/libraries目录中的现有库。

希望对你有用。

jhdbpxl9

jhdbpxl92#

此外,如果您喜欢或需要使用
$this-〉智能-〉显示('test. tpl');

首先,请将“ci_smarty”对象克隆到Controller __Constructor()函数上的“smarty”中$this-〉smarty =克隆$this-〉ci_smarty;

相关问题