Adding twig in slim framework

Ok, here how i worked to implement this,

First i added twig in require section of composer.json

"twig/twig":"*"

then i update my vendor using composer update.

after that, in my index.php, i added the following lines

$loader = new Twig_Loader_Filesystem('application/views/');
$twig = new Twig_Environment($loader, array(
    'cache' => 'cache',
));

note that, i am storing my view files in application/views folder and will store cache in cache folder.

at this point, i created a test route

$app->get('/twig',function(){
    global $twig;
    $data = array('inp'=>1);
    echo $twig->render('twig_test.twg.php', $data);

});

the view file (twig_test.twg.php) is pretty simple,  i just added this line in view file to test that my twig configuration is working well.

{{inp}}

then finally, boot up the php development server using php-S and went to localhost/twig to confirm that twig is working with slim framework

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s