Reputation: 5409
I'm using the Slim framework for a simple website I'm developing and would also like to use the Twig templating engine.
I've installed Slim, Slim Extras and Twig and my index.php
file has the following content:
<?php
/* Require and initialize Slim and Twig */
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
require 'Twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
$app = new \Slim\Slim(array(
'view' => new \Slim\Extras\Views\Twig()
));
/* Application routes */
$app->get('/', function () {
$pageTitle = 'hello world';
$body = 'sup world';
$app->view()->setData(array('title' => $title, 'body' => $body));
$app->render('index.html');
});
/* Run the application */
$app->run();
However, this results in an error: Fatal error: Call to a member function view() on a non-object in C:\xampp\htdocs\index.php on line 18
I'll admit I have no idea how to properly use Twig with Slim. I followed a StackOverflow post on the directory structure and how to load Twig, but I don't know how to add some custom data to a view and I don't know where the view files for Twig should be stored.
I googled but a lot of the information is for older versions of Slim and the Slim documentation itself is lacking.
Thanks!
Upvotes: 4
Views: 11567
Reputation: 83
Newer Slim framework (I believe 2.2+) layout come as composer components. And Twig documentation is a bit vague as to where to put what and how to explain it to objects. Took me some time to figure out how to initialize simple file system environment. Hope it helps if someone steps on this question:
Example:
In a working/current folder I have following folders/files:
templates
home_page.html
vendor
slim
twig
.
.
index.php
composer.json
Templates
is where I will put template files for Twig, vendor
is folder where components reside (Slim, Twig ... the rest), index.php
is Slim application and composer.json
is control file for the composer.
My initialization looks as follows:
<?php
require_once 'vendor/autoload.php';
// require 'Slim/Slim.php'; (old way)
\Slim\Slim::registerAutoloader();
$loader = new Twig_Loader_Filesystem('./templates');
$twig = new Twig_Environment($loader /* ,array(twig config) */);
$app = new \Slim\Slim();
note Twig_Loader_Filesystem with loader (took me some time to figure out what is wrong since Twigs first example uses $loader = new Twig_Loader_String();
and it would not render from a file until you spot the fact that you have wrote one thing and expect to do another.
rendering goes as follows:
$template = $twig->loadTemplate('home_page.html');
echo $template->render( array( /* values for template */ ) );
or:
echo $twig->render('home_page.html',array( /* values for template */ ) );
do not forget to: function ( /* params */ ) use($app, $twig) as to reach twig object.
Security: Note that is safer to make .php templates than .html as Slim does not cover/protect folders that exist in the file system so for example apache will send files directly not consulting Slim at all, alternatively, place templates out of the web space.
Upvotes: 2
Reputation: 34105
This is a simple php error. You're trying to access a variable in a closure that's not imported into its scope, fix it like this:
$app->get('/', function () use ($app) {
// $app will work here
});
Upvotes: 15