1 00:00:00,000 --> 00:00:05,897 [MUSIC] 2 00:00:05,897 --> 00:00:12,593 [SOUND] Hi, Alena here, to talk about templates. 3 00:00:12,593 --> 00:00:17,508 A template is a pattern used for making accurate copies of something. 4 00:00:17,508 --> 00:00:22,773 A template engine is software designed to combine templates with a data model 5 00:00:22,773 --> 00:00:27,722 to produce multiple pages that share the same look throughout the site. 6 00:00:27,722 --> 00:00:31,760 These are the views in an MVC project. 7 00:00:31,760 --> 00:00:35,124 MVC stands for Model-View-Controller and 8 00:00:35,124 --> 00:00:40,891 is one of the most important design patterns for organizing an application. 9 00:00:40,891 --> 00:00:44,777 The model represents the data in your application. 10 00:00:44,777 --> 00:00:49,600 The view is the visual component that presents the interface for 11 00:00:49,600 --> 00:00:52,198 users to interact with that data. 12 00:00:52,198 --> 00:00:57,826 The controller is responsible for coordinating what specific actions 13 00:00:57,826 --> 00:01:04,403 need to be performed in order to return the correct response for a user's request. 14 00:01:04,403 --> 00:01:07,615 For more information on the MVC design pattern, 15 00:01:07,615 --> 00:01:10,602 check the notes associated with this video. 16 00:01:10,602 --> 00:01:14,597 This course will be focusing on the view portion of MVC, 17 00:01:14,597 --> 00:01:19,360 which is the visual component, the templates of an application. 18 00:01:19,360 --> 00:01:24,175 Many people will tell you that PHP itself is a template engine. 19 00:01:24,175 --> 00:01:27,257 Even if PHP started as a template language, 20 00:01:27,257 --> 00:01:30,184 it has taken a much different direction. 21 00:01:30,184 --> 00:01:34,892 This isn't to say that PHP can't be used as a template engine. 22 00:01:34,892 --> 00:01:40,010 In fact, there's a wonderful package to help PHP to do just that. 23 00:01:40,010 --> 00:01:44,554 When creating an MVC application, your views should be 24 00:01:44,554 --> 00:01:49,203 restricted to the display or rendering of a page itself. 25 00:01:49,203 --> 00:01:54,801 The only logic that should be used is a structure that directly relates to 26 00:01:54,801 --> 00:02:00,855 the display of the data, such as looping over over a list to display each item, 27 00:02:00,855 --> 00:02:04,647 or deciding if something should be shown or not. 28 00:02:04,647 --> 00:02:08,288 By keeping our code organized in this fashion, 29 00:02:08,288 --> 00:02:11,845 we make it easier to add features and fix bugs. 30 00:02:11,845 --> 00:02:17,214 We also make it easier for multiple people to work together on a project 31 00:02:17,214 --> 00:02:22,042 while specializing in a specific portion of the application. 32 00:02:22,042 --> 00:02:27,210 Using a templating language for views restricts the available logic, 33 00:02:27,210 --> 00:02:30,914 while providing a clean and simplified language, 34 00:02:30,914 --> 00:02:36,441 that makes these templates more accessible for non-programmers as well. 35 00:02:36,441 --> 00:02:40,787 A template engine may also provide greater security, and 36 00:02:40,787 --> 00:02:46,109 flexibility for your layouts, by providing multiple inheritances, 37 00:02:46,109 --> 00:02:49,845 blocks, automatic output escaping, and more. 38 00:02:49,845 --> 00:02:55,011 Instead of each individual view, putting together the pieces it needs, 39 00:02:55,011 --> 00:03:00,591 such as the header and the footer, each view can extend from a master template, 40 00:03:00,591 --> 00:03:04,612 and simply replace the portions that need to be changed. 41 00:03:04,612 --> 00:03:08,056 There are many options when choosing a template engine. 42 00:03:08,056 --> 00:03:11,128 This course will demonstrate the use of Twig, 43 00:03:11,128 --> 00:03:14,292 one of the most common template engines in PHP. 44 00:03:14,292 --> 00:03:18,934 Twig is built by the same people that developed the Symphony framework. 45 00:03:18,934 --> 00:03:23,370 It provides a flexible, fast and secure template engine, 46 00:03:23,370 --> 00:03:25,686 while being well documented. 47 00:03:25,686 --> 00:03:29,670 I'll be showing you how to use Twig as a standalone package, 48 00:03:29,670 --> 00:03:33,824 while also sharing examples of integrating with Frameworks. 49 00:03:33,824 --> 00:03:38,143 There isn't much difference between these two approaches, 50 00:03:38,143 --> 00:03:42,057 because our views should be kept as a separate concern. 51 00:03:42,057 --> 00:03:45,471 Let's get started setting up Twig for our project.