Php advanced template engine tutorial




















Imagine a typical case of building a user's profile page. Let's assume we need to display the user's photo, username, real name, age and location. An example HTML is provided below. Create your template file and save it.

I like to use the tpl extension. For easier use and portability we'll need a class - it will be called Template. This class will only need two member variables - one for storing the filename of the template and the other to store the values that will be used for replacing the tags in the template. Let's start with this. We'll define our class and its constructor.

I provide the code for this bellow. I'll refrain from putting the comments in the code snippets I provide here, because they are lengthy, but the source code for this tutorial is well documented with comments. At least two more methods are needed: one for setting the values for each tag and the other for outputting the final result.

We'll store our values in a PHP array where each value is stored with a key that matches the tag it is supposed to replace. The method for outputting the content is a little more advanced called output. We'll start by trying to verify if the template file exists. If we're unable to do that we'll return an error message. If the template exists we'll load its contents into a variable.

We then loop through our values array and replace each tag with its matching value. Now we can finally test our first iteration of the template engine. We'll start by including the file with the definition for our Template class I called mine template. We then make a new Template object and we define each value in the template. In the end we want to write its output. You can see that this is very simple to use.

In a real world example those values would be previously loaded from a database or file. Something is missing though. What we're missing is our layout. Filters are applied to the variable to change its contents and print it to the template. Take the following code:.

This will affect the variable being printed, and all lowercase letters will be switched to uppercase. Filters can be chained, too. So, you could strip out any HTML tags and convert the variable to uppercase with following code:.

In templates, most of the operations you will be doing will be looping over a set of data listing some items , sometimes accompanied by conditional statements if … then. This is why Twig has implemented only for and if control structures, but they are quite powerful. The for loop was presented in its simplest form in the previous section. Below are some more sophisticated examples.

Notice the else block. It is rendered when the array is empty and no iteration takes place. You may have noticed that the author of Twig was highly inspired by the behavior of the for structure in the Python language. Important: you cannot use break or continue statements in the Twig version of the for loop. On the other hand, you do have access to a few useful variables inside the loop:. The if statement is almost the same as it is in pure PHP.

You can use if , else and elseif tags. For example:. As with the for loop, the operators in Twig are highly inspired by the ones in Python. This covers most of the basic syntax of Twig. To go deeper, you should read the extensive documentation. As the creator of Twig states, template inheritance is the most powerful and important feature of Twig. It lets you define a main template as the base of other templates depending on the business logic.

For example, you could define the base template below and save it to the file layout. Then, by calling the template that extends layout. Template inheritance could help you create a flexible template architecture. Read the documentation to learn the details. Because Twig is so flexible an engine, there is no one specific way to incorporate it into your project.

You just have to place the Twig library somewhere in your project and create directories for the cache and templates. The locations of all of these are up to you.

Furthermore, you can define a PHP script as an entry point to your application, or include and instantiate the Twig object in every script in your project. I hope you enjoy working with template engines such as Twig and that they increase your productivity and the cleanliness of your code. Good luck! Just the things you can actually use.

Everything TypeScript, with code walkthroughs and examples. And other printed books. About The Author Krzysztof Rakowski krzrak has over twelve years of experience in designing and developing web applications in different technologies.

Email Newsletter Your smashing email. New Adventures in Front-End, Edition. More after jump! With slice filter, we can loop over a part of the array. We can combine the for tag with the if tag and else tags. We output the user's name of the user. When there are no active users, the output from the else tag is shown.

We sort the array with sort filter and assign the sorted array to the sorted variable with set. For instance, if we had a tutorial explaing some Twig tag, we would need not to parse a piece of the demonstration.

The format filter formats a given string by replacing the placeholders. It works like the sprintf function. Users could potentionally add dangerous input to the application. The inclusion of unknown JS file can be prevented with autoescaping. To deal with empty, blank, and null elements, Twig has empty and null tests. Twig's template inheritance is a powerful feature which removes duplication and promotes maintenance.

This is the inheritance. It renders the derived.



0コメント

  • 1000 / 1000