Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Although their capabilities are limited, template languages use the same basic concepts as any other programming language: variables, loops, conditionals and functions. Twig works nicely with most frameworks, however, because of their limited functionality, most template engines work similarly, so don't be intimidated to try other options.
Installing Twig with Slim
The slim/twig-view component allows you to render Twig templates instead of PHP templates in your application.
Step 1: Install slim/twig-view. The easiest way is through composer.
composer require slim/twig-view
Step 2: Next, you need to changed the registered view component on the Slim appβs container like this:
// Get container
$container = $app->getContainer();
// Register component on container (your view or renderer)
$container['view'] = function ($c) {
$settings = $c->get('settings')['renderer'];
$view = new \Slim\Views\Twig($settings['template_path']);
return $view;
};
Alternatives to Twig
Besides Twig, PHP has a number of other templating engines available.
Blade is the template engine used by Laravel, a PHP framework that has quickly risen to become one of the top-used PHP frameworks in the world today. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.
Mustache is a template engine that is available for nearly any language you can think of. Next, it is advertised as logic-less, which basically means that it only contains the absolute baseline of logic a template engine can have (token replacement, for each, if null). They leave it logic-less intentionally, perhaps for ease of portability to all those languages, but perhaps also to take the concept of clean views to an extreme for those who consider that their top priority.
Smarty gives you the tools to make a clean separation of presentation from application code, it also gives you plenty of room to bend those rules. A poor implementation (i.e. injecting PHP in templates) will cause more problems than the presentation separation was meant to resolve. The documentation does a good job of indicating what things to watch out for.
For a different approach, Plates is a native PHP template system thatβs fast, easy to use and easy to extend. Itβs inspired by the excellent Twig template engine and strives to bring modern template language functionality to native PHP templates. Plates is designed for developers who prefer to use native PHP templates over compiled template languages, such as Twig or Smarty.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up