Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using Slim framework and Twig template

Tags:

php

twig

slim

I'm trying to make Slim work with Twig template system, this is part of my index.php

// Twig [Template]
require 'Extras/Views/Twig.php';
TwigView::$twigDirectory = __DIR__ . '/vendor/Twig/lib/Twig/';

//Slim
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim(array(
    'view' => $twigView
));

And this is my structure

Extras
    |_Views
        |_Twig.php
Slim
templates
vendor
    |_Twig
        |_lib
            |_Twig
index.php

I try several times with other configurations and searching buy I ALLWAYS get this error:

Fatal error: Class 'Slim\View' not found in C:\wamp\www\slim\Extras\Views\Twig.php on line 43

Can anyone help me here? All the examples I had found was using composer

like image 860
Gonz Avatar asked Apr 26 '26 14:04

Gonz


2 Answers

Ok, I solve it. This is the solution:

// Slim PHP
require "Slim/Slim.php";
\Slim\Slim::registerAutoloader();

// Twig
require "Twig/lib/Twig/Autoloader.php";
Twig_Autoloader::register();

// Start Slim.
/** @var $app Slim */
$app = new \Slim\Slim(array(
    "view" => new \Slim\Extras\Views\Twig()
));

And this is my structure now.

Slim
|_Extras
    |_Views
        |_Twig.php
|_Slim
templates
Twig
 |_lib
   |_Twig
      |_Autoloader.php
index.php

¡I hope this help someone else!

like image 165
Gonz Avatar answered Apr 29 '26 03:04

Gonz


Now Slim-Extras is DEPRECATED, we must use Slim-Views (https://github.com/codeguy/Slim-Views):

require "Slim/Slim.php";
\Slim\Slim::registerAutoloader();

$slim = new \Slim\Slim( array(
        'debug' => false, 
        'templates.path' => 'fooDirTemplates', 
        'view' => '\Slim\Views\Twig'
    ));

$twigView = $slim->view();
$twigView->parserOptions = array(
        'debug' => false
    );
$twigView->parserDirectory = 'Twig';
$twigView->parserExtensions = array(
        '\Slim\Views\TwigExtension'
    );

$slim->notFound( 'fooNotFoundFunction' );
$slim->error( 'fooErrorFunction' );

// SLIM routes...

$slim->run();
like image 24
Virgili Garcia Avatar answered Apr 29 '26 03:04

Virgili Garcia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!