我正在使用CodeIgniter 3.1.8和Bootstrap 4开发在线报纸/博客应用程序。我决定添加主题。应用程序是而不是HMVC,只有MVC。
我认为使用Twig模板引擎添加主题是个好主意。为此,我使用CodeIgniter Simple and Secure Twig。
主题的模板具有扩展名.twig
,而不是.php
。
在帖子控制器中,我有这两种方法用于所有帖子和帖子由作者过滤:
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
片段,我用它来显示所有的帖子和按作者过滤的帖子:
<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
属性,我需要一个基url。
在applicationconfigconfig.php
中,我有这个片段来动态获取应用程序的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模板。
我该怎么做?
我找到了一个简单的解决方案:
首先,我在两种方法中都向$data
数组添加了一个base_url
变量:
public function index() {
//call initialization method
$config = $this->_initPagination("/", $this->Posts_model->get_num_rows());
$data = $this->Static_model->get_static_data();
$data['base_url'] = base_url("/");
$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['base_url'] = base_url("/");
$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);
}
然后我在post.twig模板中使用了它:
<div class="spotlight {% if (loop.index is even) %}alt{% else %}{% endif %}">
<div class="image flush">
<a href="{{post.slug}}">
<img src="{{base_url}}/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="{{base_url}}{{post.slug}}" class="button special small">Read More</a>
</div>
</div>
</div>
如果你有更好的解决方案,请告诉我。谢谢