简体   繁体   中英

using twig in codeigniter project

I have a project that I have set up using codeigniter and used composer to install twig. I am new to codeigniter and composer and am not sure what to do to start using twig in my views. I have created a file in the views folder called resources/master.html and one called index.html.

In index.html I have just put

{% extends "resources/master.html" %}

which is simply being rendered on the page as text, seems as if twig is not being loaded. What do I need to do to get twig to interpret the page?

I am not sure how to incorporate Composer and Codeigniter. I suspect you might need to find/write a package that autoloads twig.

I installed a Codeigniter specific package manager, sparks and used that to install/integrate Twig via a wrapper package called twiggy . You cd into your Codeigniter project's root directory. Install sparks:

$ php -r "$(curl -fsSL http://getsparks.org/go-sparks)"

Then you can install the twiggy package. You'll see it in the sparks/ directory where you can see what version was installed:

$ php tools/spark install twiggy
$ ls sparks/Twiggy/
0.8.5
$

Then set up the directory structure for twig

$ mkdir -p  application/themes/default/_layouts

And then,optionally, you can update Twig to the latest version or version you want. The one in twiggy seems a little old :

$ cd sparks/Twiggy/0.8.5/vendor/
$ rm -fr Twig/
$ git clone https://github.com/fabpot/Twig

The twiggy link , has an example of templates you can use to test Twig.

Here's an example controller, application/controllers/test.php , using them and passing data as well (more on that here :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends CI_Controller {

    /* */
    public function __construct(){
        parent::__construct();
        $this->load->spark('Twiggy/0.8.5'); //enable Twig
    }

    public function index()
    {
        //set a data variable to pass, tell twiggy which template to use 
        //application/themes/default/index.html.twig, here

        $this->twiggy->set( 'data', array('name' => 'index') )->template('index')->display();
    }
}

Browsing to /test/index , you'll see the rendered result from your Twig template.

There is a twig-codeigniter project with sources where you can find answers on your questions. Take a look at this page https://connect.sensiolabs.com/profile/bmatschullat/project/twig-codeigniter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM