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 trialTony Petroski
1,789 Pointstemplate folder routing?
Hi All,
May someone explain how this step pointed the server to index.html file under the templates file folder?
$app->get('/', function() use($app){ $app->render('index.html'); });
I would have expected something like get('/templates', function () etc
And don't forget, we also had an index.html file in the root. So im not understanding how we routed to the other /templates/index.html
thanks,
Tony.
2 Answers
thomascawthorn
22,986 PointsHey Tony,
For this, you'll need to look into the Slim source.
Go to vendor/slim/slim/Slim/View.php, you're looking for the render method on line 268.
<?php
protected function render($template, $data = null)
{
$templatePathname = $this->getTemplatePathname($template);
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 eventually what get's called on $app->render() method.
You'll notice it calls 'getTemplatePathname'. In that method (up the page) it prefixes the templates directory to the template file name (in this case index.html).
The templatesDirectory is set from a default value (vendor/slim/slim/Slim/Slim.php line 285) under [templates.path]. The default value is './templates', which is why you don't need to pass the path into the render action.
You can customise these settings when your initialise a new instance of Slim. Essentially, you just pass in an array to the construct and it will override the defaults - (see here)[http://docs.slimframework.com/configuration/settings/] for more info!
It felt pretty massive when I started looking into project sources - let me know if you need any more help looking around :)
Tony Petroski
1,789 PointsThanks for the detailed response Tom.
Since i posted, i have kept practising with routing to get a better handle of the process. Making better sense now. I talked about get methods references like get('/templates', function () which is incorrect. I can see that now. We don't want the web user to see this routing. As you state, the default class setting directing to /templates is preferred so the directory filename won't show up on the URL title. I will revisit the class as you recommend.