解压 smarty 得到文件列表为
├── CHANGELOG.md
├── composer.json
├── COMPOSER_RELEASE_NOTES.txt
├── demo
├── expectException
├── INHERITANCE_RELEASE_NOTES.txt
├── lexer
├── libs
├── LICENSE
├── NEW_FEATURES.txt
├── README
├── README.md
├── SMARTY_2_BC_NOTES.txt
├── SMARTY_3.0_BC_NOTES.txt
└── SMARTY_3.1_NOTES.txt
将 libs
移动到 Codeigniter4 的 app/ThirdParty
目录下,并将其改名为 smarty
配置 app/Config/Autoload.php
文件中的 classmap
项,使其如下:
public $classmap = [
'Smarty' => APPPATH . 'ThirdParty/smarty/Smarty.class.php',
];
配置控制器公共文件 app/Controllers/BaseController.php
,初始化 smarty
:
private function setSmarty()
{
$this->smarty = new \Smarty();
$root = sprintf('%s/../', FCPATH);
$this->smarty->setTemplateDir($root . 'default/');
$this->smarty->setCompileDir($root . 'writable/smarty/templates_c/');
$this->smarty->setConfigDir($root . 'writable/smarty/configs/');
$this->smarty->setCacheDir($root . 'writable/smarty/cache/');
}
initController
设置 smarty
初始化:
$this->setSmarty();
在控制器对应的方法使用:
$this->smarty->assign('name','Ned');
$this->smarty->display('index.tpl');