 Okay. Welcome everyone. So, this session is about twig. I'm not going to talk about twig in two technical terms, really. What I want to do today is talking more about the philosophy behind twig, and I want to emphasize one big feature of twig, which is how you can organize your templates. But first, I also want to talk about why you need a templating engine in the first place and which problems we are trying to solve with a templating system, and why web designers should embrace a templating system like twig. Okay, so this quote comes from a blog post I wrote back in 2009 when I started to work on twig. It was the first time I was talking about twig in public, and at that time people were very scary about the idea of using a templating system in PHP, probably because in all the major frameworks, the templating system used by this framework was actually plain PHP. So, a very long time ago, SMARTU was really popular, but at that time, in 2007, 2009, people were sure that the best templating system was PHP and that you don't really need another one. And even if I was really convinced that using a templating system like twig was really a good idea even for frameworks like symphony, I thought it would be quite impossible to convince the PHP world that using twig was actually a good idea. And, of course, I was wrong because three years later, twig is used by many people. So, the first stable version of twig was released in March 2011, two years after the announcements of the twig project, so it took a long time to get there. But we took the time because even if the project is really active, we had almost 40 releases since then, so 18 months and 36 releases, that's a lot, but zero backward compatibility breaks. So, even if we had 36 versions, we did not break backward compatibility a single time. And that's something I'm really proud of and we try very hard to not break backward compatibility. That's true for twig, the language itself, so the thing that you are using to create a template, but that's also true for the PHP side of things. And today I'm not going to talk about the PHP side of things, I'm really just going to talk about PHP, the templating system, the templating language, really. So, we have about 140 different contributors for the core. And since the, so I'm not sure you are familiar with packages, packages is the website where you can get stuff that are instatable via Composer, Composer being the package management system that is going to be used by Drupal version 8. And packages started a year and a half ago and during that period of time, twig was downloaded more than 1.5 million times. So, that's a lot really and we were actually the first project to hit 1 million downloads on packages. So, if you want to get more information about twig, you can get on the official website, twig.sensualabs.org website. We have a bunch of documentation. That's also why I'm not going to talk about all the details of twig because you can get the documentation pretty easily and I think everything is well documented. And if you want to contribute, that's really easy. This is the GitHub account where you can go. You can fork the project. You can contribute documentation. That's always nice to have web designers only people trying to fix the documentation because obviously I'm not a web designer, I'm a developer. Sometimes I explain things from the developer point of view and the web designer point of view can be quite different. So, I'm always looking for more people contributing to the docs and tweaking things. And if you just want to send some love to the twig project, you can start the project also. So, when we are talking about terminating system, we are talking about HTML websites, but not only HTML websites. We are talking about things that are displayed to the user or that we send to the user. So, it can be a web page, of course. It can be an RSS feed, it can be an API, an XML API, it can be emails. So, that's not just about HTML. And twig does not solve only the HTML problem, the terminating system for HTML. It tries to solve the problem at another level, really. Here, this is a very simple example. An email I received some time ago. It's about a license from ZenServer. And as you can see, the email is probably the same for all the people buying the ZenServer stuff. It's more than the same for everyone, but some parts are customized based on the recipient. So, if the email was just about static content, then we would not need to use a terminating system. We need a terminating system because we have some placeholders, really. So, some stuff that are different based on the recipient of the email. So, if we were to use PHP for this email, this is how you would add, change the placeholder and replace the placeholder with the value of the username here. So, as you can see, this is quite verbose. And if you know nothing about PHP, that's probably just a lot of random characters, really. Why on earth do I need to put all these random characters to make it work? It does not make any sense, really. So, it's error-prone because you need to remember the exact syntax you need to use and you need to use these syntax every time you need to output something. You need to replace a placeholder value whenever you need to inject the content. So, it does not really make sense. It's not web designer friendly, really. And actually, you can explain why it is done this way. But then you need to understand PHP or you need really to understand XML, really, because this is really an XML notation, right? And here we are in a text email. So, it does not really make sense to use an XML notation in an email, which is just plain text, right? But this is the PHP way. So, PHP was created a long time ago as a templating system for HTML and probably just HTML. It was not really created for anything else. And if you have a look at the evolution of PHP in the last five years, you can probably understand that PHP did not evolve as a templating system. It evolved as a language, a programming language, actually. If you have a look at the new stuff in PHP 5.3, 5.4, 5.5, I think you can find just one thing that makes PHP a better templating system. So, that's why I think that PHP is not a templating system anymore. It's not the best way to create your templates. And even if you can use it as a templating system, it's just plain ugly. Yeah. So, actually, there was one thing. I think it was in PHP 5.4 where you can actually remove the PHP error. So, that's a bit better. I'm not going to explain why it was not the case before and now. You can't do that. But still, it's just about a lot of different randoms character. So, wouldn't it be better to replace those random characters with something shorter? And as you can see, it's just about random characters again. But at least it is shorter, easier to understand, consistent. And that's tweak. So, in tweak, when you see double braces like this, it means it says to tweak output what we have in between. So, here, you want to output the name of the user. So, this is a convention. And as you will see later on, we have few conventions in tweak. But even if you can think that those double braces are random characters, actually, the syntax is not really random because tweak, the tweak syntax, is actually used and shared with many other PHP projects. So, Drupal being one of them, Drupal version 8, but as you can see on the screen, there are many other PHP projects that are actually using the exact same syntax because they're using tweak. And even if you're not using PHP, if you are using Python or Ruby or JavaScript, you can find similar templating system with the exact same syntax, which means that if you are going to learn tweak, you will be able to reuse the same syntax, your new knowledge in Python, in Ruby, and in JavaScripts. JavaScript being something quite interesting because it means that you can share templates between your front-end and your back-end. You can use the same template to generate HTML pages from a back-end and you can use the same in the front-end. So that's quite interesting to be able to use the same template on different technologies, really. Okay, so we saw an example where it was just about outputting something, just one place older. This is an example. It's quite common to iterate over items and echoing something and if there are no items, then you have sentencing data and there are no items, really. And as you can see, that's a lot of random noise, really. It's hard to understand what's going on. It's hard to understand what is going to be displayed, how it works. If you have a look at the same in tweak, first it's half the size of the previous one, right, half the size. And then, even if you know nothing about tweak, I've not talked about these random characters yet, but you can probably understand what's going on and you can probably figure out what is going to be output to the user. So the first thing I want to talk about is the core concepts that we have in tweak. And as you will see, we don't have that many different concepts. So the first thing, and we've already talked about a bit about that, the first thing you want to do in a template is to be able to say something, to output something, to replace a place older with a value, to inject a value in the template. And as you know now, this is how you can do that. Double braces, what you want to display, the name value, and you close the double braces. Now, sometimes you want to modify the content, right? The way you can modify the content that is displayed to the user is by using what we call filters. And a filter is what you put after the pipe, right? And here we say we want the uppercase version of the name, right? You have the output at the bottom of the screen, right? So a filter is a way to modify the value that comes before the filter, right? And you close with many different kind of values. So here I have a tag, a set of tags. You can join the tag. It's going to join all the values, all the items in the tags, in the tags variable, and then you have a string, because of course you can only output strings. You can't really output a set of an array. And sometimes the filters can take arguments. The way you can pass arguments to a filter is by using parentheses, and then you pass as many arguments as you need to. So here I say, okay, I want to join all the tags, and I want the tags to be separated by your command and space, right? Pretty easy to understand. I'm not going to show you all the filters we have, but this one is kind of interesting, because it really says a lot about the philosophy of twig and how we try to add new feature to twig. So here I want to display the number of tags we have. So I use the length filter. Pretty simple. The cool thing is that you can also use the length filter for a string, right? It does work, which means that many filters, that's not the only one, we have a bunch of other filters that can work for strings, arrays, objects, whatever. So as a web designer, you don't need to understand which filter to use based on the type of the variable. You can't just say, I want the length of this variable. I don't care if it is a string or an array or an object or whatever. So first it means that filters and many features in twig are predictable. And then we try to be very consistent, which is not the case in PHP, where you have to use Esther length for a string and count for an array. So you have different constructions for different kind of variables. So another example, you have a description. A description has some tags. You don't want the tags to be displayed, probably because you are in an email or whatever. So you can strip the tags, same as the PHP function, really. But the interesting thing is that you can chain more than one modifier, more than one filter, really. So here I say, okay, I want to strip all the tags from the description and then I want the uppercase version of this description. So you can chain as many filters as you need. So to sum up, a filter is something that you can use to modify a variable, to modify something that is going to be output to a user. And here you can see all the built-in filters we have in twig. You can add more, it's quite easy, but you need to write some PHP code. But by default, we have a bunch of them. We saw a couple of them, but you can go to the twig website to get more information about the other ones. And actually a small tip here. Whenever you want to know something about a filter or a tag or whatever we'll see later on, you can go to the twig.sensealabs.org website and then replace xxx with the name of the filter. You don't need to remember the path. This is the same as the PHP.net website, really. So just go to twig.sensealabs.org, add whatever you want to look for and you will be rejected to write a page of the documentation. So the first thing is about saying something. Then you want to access the data and the variables that are exposed in a template. So in the PHP code you expose, you pass some variables to the template and then the template is going to be able to access the data to display stuff, right? And in twig, we have the dot notation. The dot notation is a way to access data on more complex variable types like arrays, objects, whatever. And the dot is really at the magic bullet here because the designer does not need to care about the type of the variable passed from the PHP code to the template. It doesn't need to know if it is an object or an array or whatever, really, or a string or whatever. He knows that if you want to get the name property of the user variable, he can just use the dot. And behind the scene, twig is going to try several different things. It's going to try to access the user as an array, if possible, and get the name item from the array. It's going to get the name property from the user object if it is an object or call the name method or even the get name method, right? So we have an algorithm that is able to check and find out which way we need to access the data from this user variable. And that's great because, for instance, when you design a new website, you can create all your templates with twig without any PHP code, and you can just declare and pass the variables as an array of strings, make it work, and then you can connect the PHP code with the twig code without changing anything, even if in the PHP code you have objects, whereas you created the templates with just plain arrays. So that's a great way to let the web designer work alone without any PHP code and then at some point switch to the real structure of the variables. And of course, you can also chain the calls. So here I'm getting the company name for the user, right? You can combine everything together. So here I am getting the addresses for the user, and then I want just the first one, and for this result, which is the first address of the user, I want the city, right? And even if it looks pretty easy, behind the scene, that's not because the user can be an object or an array. The addresses method or item from the array can be an array of objects or an array of arrays and so on. So the number of combinations is really high. And that's why this call, the dot in twig, is the slowest part of the engine, really. And that's also why we created a C extension. So if you want to get the most speed out of twig, I encourage you to install the twig C extension. It's not going to replace all the twig implementation. It's just about replacing the implementation of the dot algorithm, really. And depending on the number of dot calls that you have in template, you can improve the performance a lot. The C extension is available in the same repository as the main twig code and compiling the C extension is documented on the twig website. So you can say something. You can access complex variables. And at some point, you can also want to use what we call expression. So in fact, when you have a double braces like that, what is inside is an expression. So when we say name, this is a variable, but this is a very simple expression, just name. If you remember the user dot addresses by first dot city, this is an expression, right? So anything in double braces is an expression, which means that you can do that, for instance. But you can, right? So we have the math operators, which can be more useful if you want to get the age plus one, whatever. So you can use operators to actually change the value of a variable. You can also concatenate strings. So here I want the first name of the user, then concatenate a space, then concatenate the last name of the user. Of course, you could also just output the user first, then a space, then output the user dot last. This is exactly the same. So this example is a bit contrived, but you get the ID. At some point, you might wonder if the item you want to get from the user is called first or first name. You don't remember. In this case, you can use a debug tool that is built in in Twig, and it's named dump. This is a function that you can call. And you pass, for instance, a variable. It's going to dump what the variable is. So that way you can get all the items that you can get from this user variable. So that's a great way to discover what you can call from a variable. You can also dump a complex expression, actually, so you can get all the variables available on the user company. And if you want to get access to all the variables that are available in a template, you can use this trick. So underscore context actually contains all the variables available in the current template. And here I'm saying I want just the keys and dump the keys. So this is a way to actually get all the variables available in the template. So here are the built-in operators. As you can see, we have a bunch of math operators. We have all the logic ones, the comparisons, and a bunch of others. I'm going to talk about later on. So the dot, the concatenate operator, the pipe for the filter, and we're going to talk about in and is later on. So now you understand the first main concept of Twig. Twig is able to output something. It outputs an expression. An expression is made of variables, variable data access, and filters. That's all there is to it, really. But at some point, you also need to be able to do something. We are talking about the presentation logic, right? We are not talking about the business logic. That's for the PHP code. That's for the model in the PHP code. We are talking about the presentation logic. So this is the other random characters you need to care about. Braces, a brace, a curly brace, percentage, something, and percentage, closing brace. So we have placeholders. Say something, and now we need to do something. But of course, as we are talking about a templating system, we need to find the right balance between giving enough features to the web designer to implement the presentation logic. But we also need to restrict all the advanced features to avoid, you know, that the business logic is actually coded in within the template. That's why you can't use PHP from Twig. And we have no options to actually switch the possibility to add PHP, and we will never add that because that's really bad. If you are using a templating system, obviously you don't want to use PHP. If at some point you think that using PHP is your only option because you can't do what you want to do in Twig, it probably means that you are not aware of a feature of Twig. First possibility, and the other one, is probably because you are trying to do something that does not belong to the template. And you need to refactor the code. You need to move this logic to the PHP code. So, and when I say do something, it means that this tag does not add put anything, right? Never ever. So I'm going to talk about two main tags that we have. The first one is four, and it allows you to iterate over things. So you say for article in articles, so this should be iterable actually, so it can be an object or an arrays, whatever. And then you do stuff, and then you have and for. So when you have things inside the tag, you always have the same convention, the tag name, and you end with an end tag name. And here the interesting stuff is that we also have this else statement, meaning that if there are no articles, then we are going to display no articles. So if you have some articles, you will have a list of all article titles. If not, you will have the no articles sentence displayed. Pretty easy to understand. Actually, when we are talking about an array in PHP, it can mean a sequence or a hash, really. So it means that for each article, you might want to get the position of the article, for instance, so you can do that with this notation. Or if you only want to get the position that you can call the keys, filter our articles, and it's going to return just the keys of this variable. Actually, after in, you can have any expression, right? We've talked about expression before. So here you can put any valid expression. So here we have all the addresses for the user. Here we have a range from 1 to 10. So this is the equivalent of saying I want to iterate over 1, 2, 3, 4, 5, 6. Up to 10, you can do the same with letters A to Z. Or you can have an array or whatever combination, whatever you want. Any valid expression can be used after the in statement. Interesting. When you are within a for loop, you have access to a special variable, which is only available when you are in the for loop. The variable is called loop. And it is automatically created by Twig, and it gives you access to a lot of interesting information about the loop. The index we are at, index 0 means that this one starts with 1, this one starts with 0. Loop first returns true if this is the first iteration for otherwise, and the last returns true if this is the last iteration of your array. And if you have nested four calls, then you can call loop.parent. It's going to return the context of the parent for loop. You can also use a condition. So you can say, I want to iterate over all the users, but only if the user edge is more than 18. So that's quite compact, and the expressiveness is really nice, instead of adding if before, which would add more noise to the code. And of course you can... Yeah, sorry. So that's the four keyword tag. The other one I want to talk about is if, and most of the time you just need for an if in the template, really. If is really similar to what you use in PHP. So if, then you have a condition. Else if, another condition, else and if. As you can see, this is the exact same convention. At the end we have and if being the tag name here. Raise it to understand. So as I said before, after the if you can have any condition. So if not user admin, if the edge is more than, you can have Boolean expression. Is, is a special. Is is a way to test something. So here we say, we want to check that the edge of the user is divisible by three. Or is even. Or odd. Or is empty. In is another interesting keyword. It means that I want to check that the user company is in this variable. Right, this rate here. And we have a lot of built-in tests really. So I've shown you divisible by empty, even, odd. We have a few other ones. And here are all the built-in tags we have in Twig. And we are going to talk about a few of them in a minute. And again, if you want to get more information about a tag or a test or a filter, whatever, you can go to twig.senseller.org slash the tag name, the filter name, the operator name, the test name, and you will be redirected to the documentation page. Okay, so that's all for the core concepts. You don't need to understand anything more than that. A way to say something, a way to do something. Expressions to express more complex stuff. That's all there is to it. That's Twig. Nothing more. Now, besides the basics, besides the core concepts, you can do cool things. And now I'm going to switch to HTML. Oh, not yet, not yet. Sorry. I want to go too fast. So the first thing I want to talk about is output escaping. This one is really important. And I think that Drupal is not there yet. But anyway, so sometimes you have a variable with some HTML, right? So this is the description of my article, for instance. And as you can see, I have some HTML tags inside. So when I want to display to output this description, it is what Twig is going to do by default, right? As you can see, all the tags are now escaped. That's because Twig tries to be secure or as secure as possible by default. Instead of asking the developer, the web designer to actually escape the variables when you output them in a template, we do that by default. We do that for him by default. But there is a but. So when we are talking about output escaping, we have more than one escaping strategy. This one is the HTML strategy. It works well when you output something in an HTML context. But if you want to output the same value in a JavaScript section, for instance, the escaping strategy is different, right? But as, I don't know, in 90% of the cases, you want to output variables in HTML. We do that by default. We are using the HTML escaping strategy because most of the time, that's what you want to do. Doesn't mean that you don't need to care about escaping quite the contrary. We do what we think is the best most of the time, but you need to think about that before outputting something. Of course, if you want to not escape a variable, you can say, I want the raw value of the desk. It's going to output the HTML, the original HTML without escaping anything. Being explicit about that means that we know that by default everything is escaped. And if something needs to be unescaped, if we need a raw value, we are explicit about it. Which means that if you want to audit, to make an audit of a bunch of templates, it's quite easy to have a look at all the raw values that you have output instead of checking all the values to see if you actually escaped them. Oh, by the way, it means that, yeah, perhaps I can show you, if you want to output the description in a JavaScript context, then you need to be explicit about that. So you need to say, okay, this description is going to be outputted in a JavaScript file or a JavaScript section. So please escape it, but using the JS JavaScript Escaper. So the escaping strategy depends on the context. And you know the context only when you output the variable. Which means that escaping variables before entering the template is plain wrong. It's plain wrong. That can't work. It can work in 90% of the cases, but for the other 10%, it won't work. That's why I think that, you know, escaping in Drupal right now, in Drupal 7, and I think it's still the same Drupal 8. The fact that everything is escaped before the variables are passed to the template is really wrong as far as security is concerned. And Twig is helping a lot here because it does the right thing by default for 95% of the cases. And then you can be explicit when you are not in the HTML context. So by the default, the built-in escapers, as you can see, we have only escapers for HTML because this is mostly what you are doing every day. And this is where we want output escaping. So we have, by default, the HTML strategy, JavaScript, CSS, HTML attributes, and URL. And it covers all the needs for HTML. So when you see that desk, by default, it means that it's going to be escaped for the HTML strategy. Whereas when you say this in PHP, it means that you are going to output the raw value without output escaping. If you want to escape this, you need to write this. Even more random characters I don't care about. I just care about the fact that I want the description here, but I need to use this very long function. And please remember to specify the car set. If not, you have a problem, you have a security problem. And of course, if you created a CMS or a framework before, you probably abstracted this so that it treats better. You created an edge function like this. But again, that's really very verbose and not really web designer friendly anyway. It's not even developer friendly, really. That's why I really think that PHP is the worst tumbling system ever. So we are talking about HTML. And at some point, when you develop a website with HTML pages, you repeat yourself a lot because from one page to the other one, most of the content, most of the structure is really exactly the same. So Tweak tries to give you tools to avoid content duplication and content structure duplication, especially when we are talking about HTML templates. So let's say that you have a sign-in block. Quite common in websites. You need to sign in to the website. So we have this block of content. This block of content does not change. The content is always exactly the same. But we want to reuse this block of content on different pages and not always at the same place. So for instance, there are a page. This is the on page index.html. As you can see, the sign-in block is here after the big heading. But on the about page, you can see that it is right at the top corner. So a way to do that, so instead of duplicating the content in the index.html and about.html page, what you can do is you can extract the code to a new file. So here I have created what you can call a partial. This is a partial template, which means that you can't really display this template to the user. It doesn't make sense. This is just a snippet of HTML code that you want to reuse in templates. So by convention, you can put them in the partial directory to make a difference between the real templates, the things that are going to be displayed to the user and the reusable parts of your design. Including this file is as easy as calling include. This is a function. I won't talk about functions, but you can think of functions as the same as filters, except that you don't filter anything. There is nothing here. There is no pipe. So the thing is include function, actually take arguments, and based on the arguments, it returns some things here. So here I want to include. I want to output the result of the inclusion of this file. So here is how you can structure your website. So I have this index.html page, the abouthtml page, and then I have a partial directory where I store all the partials that I'm going to reuse in different places of the website. But remember, the content is always exactly the same. The structure is always exactly the same. Sometimes in different places, you can have different variables. So you can be explicit about which variables you want to pass to the template. So by default, the main templates has some variables. And when you include a partial, when you include another template, it's going to inherit from the same variables. But if you want to pass some other ones, you can do so. So here I say, OK, I want to pass the login variable, which I need in this signing.html partial. And here I have the user, so I just call user login to pass the login variable to the partial. OK, so you can also pass more than one template here. That's kind of interesting. So what Twig is going to do is it's trying to try to include this user underscore sending out HTML template. And if it does not exist, it's going to fall back to this one. Why is it useful? If we are talking about a CMS, for instance, you create a default sign-in template, this one. This one is stored on disk, on the file system. But you also want to allow the webmaster or the website to actually change customers, these files. Instead of overwriting this template, which is not really a good idea, what you can do is to create a special loader in the PHP code that gets templates from the database, for instance. So in the back end, the webmaster is able to create a template based on this one. We store it in the database. And then Twig, if the template exists in the database, it's going to use it. If not, it's going to fall back to the default built-in sign-in.html template. That's quite useful. So let's come back to our website. So we have this index.html page. We have this about.html page. And as you can see, some of the contents are shared on all pages. The footer and the header are always the same. But it's not just about the header and the footer. It's really about the structure of the page. The structure of the page is exactly the same. What changes is actually what is displayed in the middle. So it's not just about the header and the footer. It's about the whole structure of the page. So even if you can use includes like this, you include the header, then you do your stuff, and then you include the footer, there is a better way. There is a better way also because if you are using includes, it means that in the header.html file, you're not going to have a full HTML structure. You're not going to have the end of the template. And the same goes for the footer.html page. So instead, what you can do is say, okay, not here, here. So for the structure in blue, this is the blue one. As you can see, this is a valid HTML page, right? And for the white space here, I don't know what is going to be displayed, but whatever, we are using these blocks in tags. And these blocks in tags means that here, it is a placeholder. We don't output anything because this is not, it's something tagged here. We say here, this is a placeholder and someone at some point needs to tell me what to put inside. What I know is that it's always the same structure here, and this part of the template changes for all pages. That's what we call a layout. A layout is an HTML file where you have some placeholders that needs to be filled somehow. How you do that? Yep, okay. So here, an example with index.html. So we have this index.html and we say, we want to extend, we want to inherit from the base layout. So we want to inherit from the structure, really, because we have the exact same structure. And now we say for the body block, which is the placeholder, where is the content? So as you can see, all the logic, all the structure, the whole structure of the page is in one place in the layout file. And then whenever I want to create a file based on this structure, I extend it, and then I create blocks to fill in what's inside. And you can do the same with the about page. So here is our structure. So we have pages. Pages can extend layouts and can include some partials. We can even specialize the main structure more. So as you can see here, we have two blocks in the content for the on page. And actually, this structure is shared on some pages. Not all of them, but some of the pages have the exact same structure. One block at the top and one block at the bottom. So we can specialize the base layout even more by creating a sub-layout. So I create a new section layout. So this is a layout. I want to extend the base layout. So the main structure is exactly the same. But then this block is... So I say that in the block body, I want to do stuff. I have a structure here, and I define two place holders. One is adding and the other one is marketing. So here it works. Here we have a layout, which means that at some point, we need to define what is the content for the adding and for the marketing blocks. But we won't need to define the content of the body block because it is defined here. So this is a way to specialize a layout even more. And you can have more than one, two or three layout extensions. So here is my index.html page again. So now I'm extending the section layout. This one contains the section blocks, but also the ones defined in the main base layout. So I have this R structure. And then I'm not going to define the body block because it's defined in section 1 template. I'm just going to say, okay, in the adding, this is the content. In the marketing, this is the content. So that's why I have layouts directory here because I can have more than one layout, and a layout can extend another one. Okay. It's going to be more and more difficult actually. So now let's say that this block has a structure. Let's say that I want to reuse this structure, but the content is going to be different. The title is going to be different and the description here is going to be different. But the structure is going to be exactly the same. So I want to reuse this block structure, but not the content. So you can't use an include, right? You need something else. If we have a look at this, so we have two place orders. So we want to have a title and a lead text. So I'm going to create a layout, partials, adding.html. So this is a layout, but for a partial. This is not a layout that is able to display a whole page. This is just about the structure of one partial. And then we will be able to create several partials with different content based on this structure. So for the index.html page, I can create a new partial, adding.index.html. So this is the heading for the index page. This is a partial. We extend the heading. Remember, probably, we had a block named title and as a one lead. So we provide the content for the title and the lead. And here is the main template. So again, we still extend the section layout to inherit from the main structure of the page. Then we define the heading and the marketing and in the heading we just include the partial that we've just created. And this partial, remember, extend a layout that defines the structure of this heading. Is it okay? Okay. You're sleeping. Okay. That's fine. But there's a problem here. This page does not make any sense anymore. We have just a bunch of extends, block, include, but the real content of the page is not there anymore. If you want to understand what is going to be displayed here, you need to go in two different files. You need to go to this file, probably because there is something here in the marketing stuff. But you also need to have a look at the adding.index.html partial to understand what is going to be displayed here. It's kind of important because the content is different on each page. So I can't guess what is going to be displayed here. It's quite different from the sign-in page, the sign-in partial because the sign-in partial is always about the same content anyway. So I know that it's going to be the sign-in block. I don't need to have a look at it. So we have too many files, right? What I want to do is I want to get rid of this adding.index.html because this one does not make any sense outside of the context of the index page. And if you do the same for the about page, it means that you would have an adding dash about.html file. So you're going to have a growing number of files. That's not a good idea. So what you can do is you can use the embed tag here. Let me explain. So again, I extend the section layout. I enter it from the main structure of the page. And then I say, okay, for this block, adding, this block, adding, I want to use the adding partial layout. And instead of creating a file, I'm going to define the blocks within this template. So the embed tag is a way to include a layout but by inlining the content you want to override. Which means that now I can have a look at this page. I can understand the structure of the page, but the content is now included in the index.html page. I don't need to create yet another file. So we have abstracted the structure of the HTML page. And you can still see the specific content for this page. Yeah, like this. So we have a bunch of blocks. At some point, especially if you have a large content, a large snippet of HTML here, it can be kind of difficult to understand which block is ended here. So what you can do is you can add the name of the block. It does not really care. I mean, it's going to check that this is the right one, or for you just to get which block you are ending here. So here in the examples I've used static pages, page names really, but again, when you are using extends or include, it is an expression. So it can be anything that is a valid expression. So here I say that HTML and here I said that a page can be, the layout of a page can be different based on the fact that the user is connected or not. So this is something that you can't do with PHP. You can't have a dynamic inheritance in PHP. Okay. We are all lazy, of course. So just want to give you a quick trip, a quick trick and a tip really. Sometimes you have this kind of structure in the HTML page. So here this is a page where I want to display some content based on the current HTTP status code. So there is an error somewhere. I want to display an error page. So I say if the status is between 400 and 500, it means that the user did something wrong. And if it is a 4.4 then we can say that this is an unknown resource. The page does not exist actually. If it is more than 500, it means that a server did something wrong. And if not, that's weird. So there is something weird happened, but we don't know what. Really. This one is quite easy to understand, but you can imagine that if you want to customize more status codes, if you want to customize I have no idea, whatever code you want, it's going to be messy with all the if statements and the nesting of the if statements. Instead, you can use this. So here I've created a bunch of pages, partials really, that are going to be included in the errors page. And I'm saying this is the main for whatever error page. So this is the base page. This is a special page for 4.4. This is the 1.4.5. And this is the default page. If nothing exists. Then I say okay, I want to include the status. So if this is a 4.4, remember if it does not exist, it's going to fall back to the next one. So if this is a 4.4 error, the 4.4.html exists so it's going to include this one. If it does not exist, it's going to fall back to this one. Here I say okay, I want to get the first character of the status code 5, add xx.html. If it does exist, if it does exist, then use that. So if this is a 4 something, it's going to use it. If it's a 5 something, it's going to use it. If not, then fall back to the default page. And now it's quite readable. And if you want to add new status code just create a new template file with the content and name being the status code. So this is just an example, really. Just to tell you that even if we don't have many concepts, all concepts can be used to actually ease your job. So whenever you have something that looks really difficult to understand, difficult to extend, difficult to customize, there's probably a way to actually simplify what you're doing. The last thing I want to talk about is, you know, at some point you will have some errors. You expect the variable to be there, but it's not there. You have some kind of problem. So you want to minimize this, their impact, really. And Twig gives you a bunch of tools for that. So for instance, we have this default filter. So I want to get the user company, but sometimes it's not defined. So by default this is the company of the user. This example doesn't make any sense. You can also ignore missing templates. So you want to include this section based on the slug. So we have a bunch of articles, and the articles are categorized in sections. And you have a template for each section. But at some point probably you messed up, you removed a slug template or whatever. You can say if it does not exist, just ignore the include and just output nothing. And I showed you how to use an array of templates for the include tag. It does work in the exact same way for the extends one. So for instance, here we say okay, if we have a local layout for the user local, so for English, whatever French, Italian, whatever, use it. If not just use the default layout. So this is a way to define fallback templates. Okay, so that's all for today. It's already time to wrap up. So this talk was really about the philosophy of tweak, the main concepts, how to organize your template, but of course there are many, many more things that you can leverage. I've listed a bunch of them here, so if you want to learn more about those stuff, they are documented on the tweak website. The sandbox mod, the fact that you can use commands anywhere. You can reuse stuff with macros and a bunch of other things really. So please have a look at them if you want to learn more. And as there are quite a few people in the room here, I'm currently working on tweak version 1.14 or something. And I'm wondering if adding new operators like this one is actually a good idea or not. How many of you can understand what they do? If I tell you that these operators actually exist in the CSS selector when you are defining a CSS selector, does it ring a bell? So now, how many of you understand these? Okay, quite a few. So for the others, the first one says I want to check if the email starts with tweak. The second one, I want to check if the email ends with tweak. And the last one says email should contain tweak. How many of you think that it should be added to tweak? Yeah, that's the problem. Every time you ask an audience if we need to add a new feature, of course, why not? So, and this is the last one I want to add. Do you understand what it does? No? No one really? One? Yeah, regular expression. So it's a way to say email whatever. So this is a regular expression. So this email matches this regular expression. Which means that you can express start with what it contains, but you have the full power of the regular expression engine, the PHP regular expression engine, actually. So how many of you think that this one is useful? Yeah, so the question is instead of adding a new operator, what about adding a new filter? We can, instead of defining these we can add a filter start with or add with or contains. The thing is a filter modifies a value. So upper you have the name and you want the uppercase version of the name, but it's still the name. We are still talking about the name. If we have a look at strip tags this is still the description at the end. So a filter modifies a value. If we add a start with filter it doesn't work anymore because it's going to return a boolean. So it's not going to return an email anymore, it's going to return something else, actually a boolean. We could use a function start with email for instance. That's why I'm asking a question. I'm not saying that I'm going to add these operators in tweak, but the fact that we have the exact same one in the CSS selectors, it might make sense to actually use the same. But then again I can understand that this is kind of yet another series of random characters introduced into the engine. So that's why I'm not really sure yet. By the way, there are some sprints on Friday so if you want to help Drupal 8 the core code or tweak, if you want to put into practice what you've learned today join the sprints on Friday. That's all. Thank you. Not sure if we have time for questions. Any questions? Any way? Yes. The question is which IDs support tweak, right? All of them. Really, you can go to the tweak on page the main documentation and you will see we have support for Veeam, TextMate, NetBeans, Eclipse you name it really. And if we don't have support for tweak if you don't have support for your ID check for Jinja support which is the same engine in Python. So you can check for tweak or Jinja but we have support for all the major IDs really and text editor actually. I will be there. So there is a tweak session, tweak lab this afternoon so if you want to join you are more than welcome and if you have any questions on tweak I will be there so I will be able to answer any questions.