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 trialKristjan Reinsberg
50,960 PointsRender .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";
});
5 Answers
Kristjan Reinsberg
50,960 PointsThanks 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
2,117 PointsThanks 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.
Geoffrey Mortstock
16,196 PointsIf 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.
- Delete your Slim directory in your workspace
- Go to your console
- Enter "composer require"
- Search for package: Slim
- Choose option 0 (Slim/Slim)
- Version Constraint: 2.6
Petrov von Petrov
21,916 PointsThe 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:
John Anderson
2,599 PointsHmmm, 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.
Utku Turna
15,090 PointsUtku Turna
15,090 PointsI also had the same issue. It should work like this in Slim 3.
First i installed slim/twig