Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Building Websites with PHP Slim Basics & Twig Templates Views & Twig Inside of Slim

Chris Ioannou
Chris Ioannou
460 Points

Error in video 'Views & Twig Inside of Slim' relating to TwigExtension.php on line 37

I get this error when I finish the video about blocks and adding a content block under the exceeds block for the main.twig within the about.twig file.

Fatal error: Class 'Twig_Extension' not found in /home/treehouse/workspace/vendor/slim/views/TwigExtension.php on line 37

I have the same versions of slim/slim, slim/views, twig/twig in my composer.json file as the instructor. I have correctly named all the blocks. I have updated the index.php file to render the about.twig file.

Here are some key snippets of code. This from my composer.json file:

   "require": {
    "monolog/monolog": "^1.22",
    "slim/slim": "2.6",
    "twig/twig": "1.18",
    "slim/views": "^0.1.3"
      }

and then this in the about.twig file

  {% extends 'main.twig' %}
  {% block content %}
  <strong>About</strong>
  <h2>Ralph Waldo Emerson</h2> <p>Ralph Waldo Emerson <em>(May 25, 1803 – April 27, 1882)</em> was an American essayist, lecturer, and poet, who led the Transcendentalist movement of the mid-19th century. He was seen as a champion of individualism and a prescient critic of the countervailing pressures of society, and he disseminated his thoughts through dozens of published essays and more than 1,500 public lectures across the United States.</p>...

and then this in the index.php file:

  $app->get('/', function() use($app){
  $app->render('about.twig');
  });

And this from the TwigExtension.php file with in he slim/views vendor folder:

  class TwigExtension extends \Twig_Extension
  {
      public function getName()
      {
          return 'slim';
      }

Anyone able to help me??

Chris Ioannou
Chris Ioannou
460 Points

And from my index.php file:

  $view->parserExtensions = array(
      new \Slim\Views\TwigExtension()
  );

  $app->get('/', function() use($app){
    $app->render('about.twig');
  });

1 Answer

Glenré Charl Labuschagné
Glenré Charl Labuschagné
23,204 Points

Hi Chris, you need to add twig/extensions to your composer.json file and run the command: "composer update" command…

"require": {
    "monolog/monolog": "^1.23",
    "slim/slim": "~2.6",
    "slim/views": "^0.1.3",
    "twig/twig": "1.18",
    "twig/extensions": "*"
}