【问题标题】:How can I pass the base_url to a twig template in this Codeigniter 3 application?如何在此 Codeigniter 3 应用程序中将 base_url 传递给 twig 模板?
【发布时间】: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>

很明显,模板中&lt;a href="{{post.slug}}" class="button special small"&gt;Read More&lt;/a&gt;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


    【解决方案1】:

    我找到了一个简单的解决方案:

    首先,我在这两种方法中都在$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>
    

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

    【讨论】:

    • 不会&lt;img src="/assets/img/posts/{{post.post_image}}" alt="{{post.title}}" /&gt;做同样的事情吗?
    • @Vickel 是的,会的。
    猜你喜欢
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 2012-09-09
    • 1970-01-01
    相关资源
    最近更新 更多