如何解决如何在此Codeigniter 3应用程序中将变量传递到Twig视图? 我如何使用主题视图使用Twig作为主题问题
我正在使用 CodeIgniter 3.1.8 和Bootstrap 4在 online newspaper/blogging application 上工作。我决定为其添加主题。该应用程序不是HMVC ,只有MVC。
themes 目录位于 application 之外,如下图所示:
在主题内部,我有一个主题目录(当然),其中包含“主视图”,layout.php
:
我如何使用主题视图
在application/core
中,我添加了一个包含以下内容的MY_Loader.php
文件:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Loader extends CI_Loader {
function theme_view($folder,$view,$vars = array(),$return = FALSE) {
$this->_ci_view_paths = array_merge($this->_ci_view_paths,array(FCPATH . $folder . '/' => TRUE));
return $this->_ci_load(array(
'_ci_view' => $view,'_ci_vars' => $this->_ci_prepare_view_vars($vars),'_ci_return' => $return
));
}
}
在我的Posts控制器的index()
方法中,我加载了传递数据的视图:
public function index() {
//more code here
$data['posts'] = $this->Posts_model->get_posts($config['limit'],$config['offset']);
$this->load->theme_view('/themes/caminar/','layout',$data);
}
使用Twig作为主题
即使我没有Symfony知识,我也认为将 Twig 模板引擎用于主题是个好主意。
为此,我使用 CodeIgniter Simple and Secure Twig 。我有:
-
添加了简单安全的树枝库:
application\libraries\Twig.php
; -
在
third_party
目录中,我添加了Twig本身。 -
在
application\config\autoload.php
中,我加载了twing:$autoload['libraries'] = array('database','form_validation','session','user_agent','twig');
-
在
application\libraries\Twig.php
中设置Twig视图的路径:
private $config = [ 'paths' => [FCPATH . '/themes',VIEWPATH],'cache' => '/path/to/twig/cache',];
问题
根据 CodeIgniter Simple and Secure Twig 的文档,我们可以像这样设置全局变量:$this->twig->addGlobal('sitename','My Awesome Site');
因此,我在Posts控制器中添加了$this->twig->addGlobal('siteTitle','My Awesome Site');
:
public function index() {
//call initialization method
$config = $this->_initPagination("/",$this->Posts_model->get_num_rows());
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
//use limit and offset returned by _initPaginator method
$data['posts'] = $this->Posts_model->get_posts($config['limit'],$config['offset']);
$this->twig->addGlobal('siteTitle','My Awesome Site');
$this->load->theme_view('/themes/caminar/',$data);
}
但是,在themes\caminar\layout.php
行中,<title>{{siteTitle}}</title>
行没有在<title>
标签内显示“我的真棒站点”。
我在做什么错了?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。