【发布时间】:2020-11-23 22:24:39
【问题描述】:
我正在使用 CodeIgniter 3.1.8 和 Bootstrap 4 开发 online newspaper/blogging application。我决定为其添加主题。该应用程序不是 HMVC,只有 MVC。
我认为使用 Twig 模板引擎添加主题是个好主意。为此,我使用 CodeIgniter Simple and Secure Twig。
主题模板的扩展名是.twig,而不是.php。
在 Posts 控制器中,我对 所有帖子 和帖子 按作者过滤 有这两种方法:
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->twig->addGlobal('maincss', base_url('themes/caminar/assets/css/main.css'));
$this->twig->display('themes/caminar/layout', $data);
}
public function byauthor($authorid){
//load and configure pagination
$this->load->library('pagination');
$config['base_url'] = base_url('/posts/byauthor/' . $authorid);
$config['query_string_segment'] = 'page';
$config['total_rows'] = $this->Posts_model->posts_by_author_count($authorid);
$config['per_page'] = 12;
if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
$_GET[$config['query_string_segment']] = 1;
}
$limit = $config['per_page'];
$offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
$this->pagination->initialize($config);
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['posts'] = $this->Posts_model->get_posts_by_author($authorid, $limit, $offset);
$data['posts_count'] = $this->Posts_model->posts_by_author_count($authorid);
$data['posts_author'] = $this->Posts_model->posts_author($authorid);
$this->twig->addGlobal('siteTitle', 'My Awesome Site');
$this->twig->addGlobal('maincss', base_url('themes/caminar/assets/css/main.css'));
$this->twig->display('themes/caminar/layout', $data);
}
看看下面来自posts.twig的sn-p,我用它来显示所有帖子和按作者过滤的帖子:
<div class="spotlight {% if (loop.index is even) %}alt{% else %}{% endif %}">
<div class="image flush">
<a href="{{post.slug}}">
<img src="/assets/img/posts/{{post.post_image}}" alt="{{post.title}}" />
</a>
</div>
<div class="inner">
<h3>{{post.title}}</h3>
<p>{{post.description}}</p>
<div class="{% if (loop.index is even) %}text-right{% else %}text-left{% endif %}">
<a href="{{post.slug}}" class="button special small">Read More</a>
</div>
</div>
</div>
很明显,模板中<a href="{{post.slug}}" class="button special small">Read More</a> 的href 属性需要一个base url。
在application\config\config.php我有这个sn-p来动态获取应用程序的base_url:
$root=(isset($_SERVER['HTTPS']) ? "https://" : "http://").$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
我无法将它传递给 Twig 模板。
我该怎么做?
【问题讨论】:
标签: php codeigniter twig codeigniter-3