What is a Template Engine?3:45 with Alena Holligan
A template engine is software designed to combine templates with a data model to produce multiple pages that share the same look throughout the site. These are the views in an MVC project.
If you are interested in PHP as a template engine, 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.
[MUSIC] 0:00 [SOUND] Hi, Alena here, to talk about templates. 0:05 A template is a pattern used for making accurate copies of something. 0:12 A template engine is software designed to combine templates with a data model 0:17 to produce multiple pages that share the same look throughout the site. 0:22 These are the views in an MVC project. 0:27 MVC stands for Model-View-Controller and 0:31 is one of the most important design patterns for organizing an application. 0:35 The model represents the data in your application. 0:40 The view is the visual component that presents the interface for 0:44 users to interact with that data. 0:49 The controller is responsible for coordinating what specific actions 0:52 need to be performed in order to return the correct response for a user's request. 0:57 For more information on the MVC design pattern, 1:04 check the notes associated with this video. 1:07 This course will be focusing on the view portion of MVC, 1:10 which is the visual component, the templates of an application. 1:14 Many people will tell you that PHP itself is a template engine. 1:19 Even if PHP started as a template language, 1:24 it has taken a much different direction. 1:27 This isn't to say that PHP can't be used as a template engine. 1:30 In fact, there's a wonderful package to help PHP to do just that. 1:34 When creating an MVC application, your views should be 1:40 restricted to the display or rendering of a page itself. 1:44 The only logic that should be used is a structure that directly relates to 1:49 the display of the data, such as looping over over a list to display each item, 1:54 or deciding if something should be shown or not. 2:00 By keeping our code organized in this fashion, 2:04 we make it easier to add features and fix bugs. 2:08 We also make it easier for multiple people to work together on a project 2:11 while specializing in a specific portion of the application. 2:17 Using a templating language for views restricts the available logic, 2:22 while providing a clean and simplified language, 2:27 that makes these templates more accessible for non-programmers as well. 2:30 A template engine may also provide greater security, and 2:36 flexibility for your layouts, by providing multiple inheritances, 2:40 blocks, automatic output escaping, and more. 2:46 Instead of each individual view, putting together the pieces it needs, 2:49 such as the header and the footer, each view can extend from a master template, 2:55 and simply replace the portions that need to be changed. 3:00 There are many options when choosing a template engine. 3:04 This course will demonstrate the use of Twig, 3:08 one of the most common template engines in PHP. 3:11 Twig is built by the same people that developed the Symphony framework. 3:14 It provides a flexible, fast and secure template engine, 3:18 while being well documented. 3:23 I'll be showing you how to use Twig as a standalone package, 3:25 while also sharing examples of integrating with Frameworks. 3:29 There isn't much difference between these two approaches, 3:33 because our views should be kept as a separate concern. 3:38 Let's get started setting up Twig for our project. 3:42
You need to sign up for Treehouse in order to download course files.Sign up