Twig library for CodeIgniter 2.x

I’m trying to catch up with this social open source movement. I forked an existing CodeIgniter Twig integration library and added some changes to work with CodeIgniter 2.x.

Also I found a way map any CodeIgniter function to Twig like so.

$this->load->library('twig'); // load the Twig library
$this->load->helper('url'); // load the CodeIgniter URL helper
// map the base_url() function as a Twig function
$this->twig->add_function('base_url');

Then in your Twig view call the base_url() function like this

{{ base_url() }}

Head over to Github for more details.

In case you have no idea what I’m talking about. CodeIgniter is an excellent PHP framework and Twig is a Django style templating engine with inheritance for PHP.

8 thoughts on “Twig library for CodeIgniter 2.x

  1. Nice, Tips..
    but.. how to automatically get all function from helper or, other library add to twig display..
    can’t we do loop for $this->twig->add_function(‘function_name’) like this ?

    $this->load->library('twig');
    $this->load->helper('array');
    for( element('user', get_defined_functions()) as $func_name ){
    $this->twig->add_function($func_name);
    }

  2. revision :

    foreach( element('user', get_defined_functions()) as $func_name ){
    $this->twig->add_function($func_name);
    }

  3. hi there..
    have you try my tips..
    but i have create a small function but helpfull, check this,

    function twg_func($func){
    $args = func_get_args();

    if(strpos($func, '.') !== false){
    $obj = explode('.', $func);
    $class = $obj[0];
    $func_name = $obj[1];

    $ci =& get_instance();
    return call_user_func_array(array($ci->$class, $func_name), array_slice($args, 1));

    }else{
    if(function_exists($func)){
    return call_user_func_array($func, array_slice($args, 1));
    }else{
    return;
    }
    }
    }

    and than you just need to this
    $this->twig->add_function(‘twg_func’);
    with this function you can all function from loaded helper or library
    when you want to call the base_url() function you just write on your twig template
    {{ twg_func(‘base_url’) }}
    it’s similiar with
    or if you want to call the class such library have loaded and store on ci object ( $this )
    you just call
    {{ twg_func(‘uri.uri_string’) }}
    it’s similiar with :
    uri->uri_string();?>
    or if you wan to pass some parameter, will be like this
    {{ twg_func(‘uri.segment’, 3) }}
    it’s similiar with
    uri->segment(3);?>

    all you have to do just separate the object with ‘.’ (dot), without ‘$this’.
    i have try it, so far not bad. :)

    nice to share with you :)

  4. Barock I tried your first solution it works but it loads Twig functions as well. The second solution looks better.

    What would be ideal is to load whatever CodeIgniter functions that are currently available excluding the Twig ones.

    I will try to make all 3 solutions available as functions in my project.

  5. You could just do $this->twig->addGlobal(‘app’, $my_ci_instance) then vola, you get access to CI object within you view. If you don’t want to expose that much in your view, restrict what to set in the addGlobal.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>