Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Building Websites with PHP Slim Basics & Twig Templates Including & Rendering

Render .html code not working

//THIS TUTORIAL IS version 2. I am useing version 3 and on localhost

    // INDEX PAGE (not working - jsut showing black page)  
$app->get('/', function() use($app) {
    $app->render('index.html');
});

// CONTACT PAGE  (working)
$app->get('/contact', function () {
    echo "tere KONATKT";
});
Utku Turna
Utku Turna
15,090 Points

I also had the same issue. It should work like this in Slim 3.

First i installed slim/twig

$app = new \Slim\App;

$container = $app->getContainer();

// Register component on container
$container['view'] = function ($container) {
    $view = new \Slim\Views\Twig('./', [
        'cache' => 'cache'
    ]);
    $view->addExtension(new \Slim\Views\TwigExtension(
        $container['router'],
        $container['request']->getUri()
    ));

    return $view;
};

// Render Twig template in route
$app->get('/', function ($request, $response) {
    return $this->view->render($response, 'index.html');
});

$app->get('/contact', function ($request, $response) {
    return $this->view->render($response, 'contact.html');
});


// Run app
$app->run();

5 Answers

Thanks Utku Turna. Code above is owrking,

Seems like Slim 3 does not allow to render html same way as in Slim 2.

Link for docs.. http://www.slimframework.com/docs/features/templates.html

Jose Navas
Jose Navas
2,117 Points

Thanks Utku Turna, the code worked for me too. I'll just add that I had to refresh the workspace to make it work right after writing your code.

If you're interested in following along with the steps the instructor provides, you can install Slim v2.6. You won't be learning the newest version of Slim, but at least you can keep up.

  1. Delete your Slim directory in your workspace
  2. Go to your console
  3. Enter "composer require"
  4. Search for package: Slim
  5. Choose option 0 (Slim/Slim)
  6. Version Constraint: 2.6

The code used in this video is useless with Slim 3, because the way it renders templates is completely different now.

You have 2 ways to go about it: either using Twig-View, like Utku Turna did, or using PHP-View.

In case you're interested, here's the code for using PHP-View (don't forget to install the component first):

<?php

require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set('Europe/Lisbon');

$app = new \Slim\App(['settings' => ['displayErrorDetails' => true]]);

// Get container
$container = $app->getContainer();

// USING PHP-VIEW

// Register component on container
$container['view'] = function ($container) {
    return new \Slim\Views\PhpRenderer('templates/');
};

// Render template in route
$app->get('/', function ($request, $response) {
    return $this->view->render($response, 'index.html');
});

$app->get('/contact', function ($request, $response) {
    return $this->view->render($response, 'contact.html');
});

// Run app
$app->run();

//That's it!

PS: If you're using Utku Turna's code, you need to path the view to the 'templates' folder, otherwise it won't find the files (unless you put them directly on the root of the project). Also you don't need the cache declaration. So when you're registering the component in the container, it would be like this:

// Register component on container

$container['view'] = function ($container) {
    $view = new \Slim\Views\Twig('templates');
    $view->addExtension(new \Slim\Views\TwigExtension(
        $container['router'],
        $container['request']->getUri()
    ));

    return $view;
};

The rest of the code would be the same.

You can learn more about it on this page:

http://www.slimframework.com/docs/features/templates.html

Hmmm, Thank you everyone. Where I am in time right now I am using Slim 3.3 and as Petrov points out, Hampton's code no longer works. Very frustrating when you are trying to learn yes? Lucky some of the examples at Slim (getting started) quickly identify some of the differences, for example: Hello world.

I have now been stuck for a long time (pulling my hair out) because Hampton says Slim will know my html files are in the Templates folder. He doesn't say how it knows and with version 3.3 I can safely say that it doesn't.

But the next lessons are on Twig and from what has been discussed above I am hoping this will clearly define the folder structure where objects live.

I know I am not adding much value here but just wanted to say thanks to those that have posted their solutions and findings so far.

Regards, John.