如何将 base_url 传递给此 Codeigniter 3 应用程序中的树枝模板?

How can I pass the base_url to a twig template in this Codeigniter 3 application?

我正在使用 CodeIgniter 3.1.8 和 Bootstrap 开发 online newspaper/blogging application 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>

很明显我需要一个base url用于模板中<a href="{{post.slug}}" class="button special small">Read More</a>href属性

application\config\config.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);
}

然后我在posts.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>

如果您有更好的解决方案,请告诉我。谢谢!