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 Including & Rendering

Slim Application Error "View cannot render 'index.html' because the template does not exist."

Ok, so this has been asked before... but there is no clear answer on the other post.

I have my file structure just as in the video... and everything else was working. When witching from just echoing to rendering... I am now getting a Slim error. As stated above, it's saying that the index.html template doesn't exist. ????

This is the code below, but I'm not sure that it is the issue, as Slim is indicating that it knows what I'm TRYING to do, it's just not finding the index.html file... which I have in a folder named templates (in the root directory).

$app = new \Slim\Slim();

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

$app->get('/contact', function() { echo 'Hello, feel free to contact us.'; });

$app->run();

Any ideas?

8 Answers

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

Brandon Gibbs you have a space at the beginning of your folder name " templates" :)

Oh my....! I can't believe that I didn't catch that... The folder structure is obviously in alphabetical order... and " templates" (space intentional that time) was at the top... above css, images, and js. Ugh.

There goes my attention to detail... :/

Thank you!!

Kevin Korte
Kevin Korte
28,149 Points

Which version of Slim are you using?

I didn't specify a specific version, so it's running the latest that stable version that it could pull - 2.6.

If you've updated to twig, try changing to index.twig :)

Ok... but the course has not yet gotten to Twig just yet... this is a Slim error, not a Twig error.

It sounds like there's something wrong with your file paths.

Slim will look up templates in the directory you can define in the settings - source. Here's more information on the template.path setting in the slim docs.

By default, templates.path is set to ./templates - you can check default settings on line 287 of vendor/slim/slim/Slim/Slim.php - so it's strange your setup isn't working!

You could try overriding to something else:

<?php

$app = new \Slim\Slim(array(
    'templates.path' => './test-folder'
));

Double check all your spelling too.

I opened that file, and it's already directing to that path on line 297.

'templates.path' => './templates',

I'm at a loss on this one.

Oh, and on the spelling part, I've gone through it all thoroughly and even had someone else look at it (just to make sure the spelling and syntax were the same from the video to my work).

This is the full index.php in my workspace at this stage of the course. Twig will be introduced in the next video.

<?php

require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set ('America/Chicago');

//$log = new Monolog\Logger('name');
//$log->pushHandler(new Monolog\Handler\StreamHandler('app.txt', Monolog\Logger::WARNING));
//$log->addWarning('Foo');

$app = new \Slim\Slim(array(
    'templates.path' => './templates'
));

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

$app->get('/contact', function() {
  echo 'Hello, feel free to contact us.';
});

$app->run(); 

Okay, try this one out:

  • Go to vendor/slim/slim/Slim/View.php
  • Go to line 271
  • You'll see this is where your exception is being thrown
  • Add a space between 270 and 271.
  • On the new line, dump out the $templatePathname -
<?php

var_dump($templatePathname); 
exit;

Double check the file path is coming through okay and everything is in the right place

I included from line 268 to the end of the file, 284.

<?php

   protected function render($template, $data = null)
    {
        $templatePathname = $this->getTemplatePathname($template);
        var_dump($templatePathname); 
        exit;
        if (!is_file($templatePathname)) {
            throw new \RuntimeException("View cannot render `$template` because the template does not exist");
        }

        $data = array_merge($this->data->all(), (array) $data);
        extract($data);
        ob_start();
        require $templatePathname;

        return ob_get_clean();
    }
}

This is the error that is produced on the page now: Parse error: syntax error, unexpected 'var_dump' (T_STRING), expecting function (T_FUNCTION) in /home/treehouse/workspace/vendor/slim/slim/Slim/Slim.php on line 272

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

Hi Brandon Gibbs,

Can you create a snapshot (icon in the top right with the preview icon) of your workspace and share it here? I am able to make your index.php file work so I need to see more details.

Of course. Workspace

Thank you.

Sorry about that. I forgot to use the proper markdown for links.

Here is the workspace.

Alena,

I don't know if you are still about but if you are I have reached the same point as Brandon above and cannot get slim to render the index.html file in the template folder.

Slim reports the following error: Method render is not a valid method

my snapshot workspace is here. https://w.trhou.se/znchpo0o31

Thanks, John.

Gerrit Verhaar
Gerrit Verhaar
17,742 Points

I got into problems because the current Slim Framework version is higher than the one used in the video. For example \Slim\Slim() no longer exists in 3.4.

To solve:

  • remove slim from workspace directory vendor)
  • remove slim from compose.json
  • reinstall slim, but this time install version 2.0: --> got to console and type in composer require slim/slim:~2.0

Now you are able to view index.html when you preview.

PS: another thing that helped me was to switch on a more detailed error message. Use this to switch it on: $app = new \Slim\Slim(['settings' => ['displayErrorDetails' => true]]);