Twig in Drupal 8
Introduction
In web development , every developer knows presenting a content in a webpage is more important than any functional logic inside the website
whether the site is for tutoring , just a blog or its site for ecommerce products , how your site going to interact with the user plays major role for your business profit .
Its a matter of minute to hold your customer/user in online to make a profit. you may thought , it is inappropriate to discuss those with the title of this post twig in drupal 8.
twig is used as presenter of your content in an easy way.
Templating Engine in drupal 7
For those who are familiar with drupal 7 already know that php is used as templating engine .
So its with an advantage of adding much more logic while creating an template for any page in drupal 7. So it is an necessity for front end developer to know about basic level of php to work with drupal 7.
Stress free Drupal 8
Every CMS 's success is identified by the number of users who are using to build a site . To make sense in this and to attract more users to drupal 8 and bind any kind of developer into drupal , twig is integrated to drupal 8.
What is Twig From SensioLabs
the flexible, fast, and secure template engine for PHP
Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum.
Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design.
Flexible: Twig is powered by a flexible lexer and parser. This allows the developer to define their own custom tags and filters, and to create their own DSL.
Twig uses a loader to locate templates, and an environment to store the configuration.
The render() method loads the template passed as a first argument and renders it with the variables passed as a second argument.
As templates are generally stored on the filesystem, Twig also comes with a filesystem loader:
Note: A template is simply a text file. It can generate any text-based format (HTML, XML, CSV, LaTeX, etc.). It doesn't have a specific extension, .html or .xml are just fine.
A template contains variables or expressions, which get replaced with values when the template is evaluated, and tags, which control the logic of the template.
Advantages of Twig:
1. Twig has a very concise syntax, which make templates more readable.
2.Template oriented syntax: Twig has shortcuts for common patterns, like having a default text displayed when you iterate over an empty array.
3.Full Featured: Twig supports everything you need to build powerful templates with ease: multiple inheritance, blocks, automatic output-escaping, and much more.
Twig Syntax:
there are two basic syntax used in twig
1.{% ... %}
To execute for loops from an array or object to build the logic .
2.{{ ... }}
It is used to print a variable value .
3. {# comment #}
Comments at the template.
4. {%set name = 'Vinodhini' %}
To set a value in a variable .
Variables can be found using the Kint or Devel modules with the {{ kint() }} or {{ dump() }} commands.
Attributes in Twig
In twig , you can provides to an template . For Example
Debugging Twig Templates in drupal 8.
you can debug a template by following the given steps
Every D8 site comes with a default.services.yml file in the sites/default folder.
1. Copy this file and rename it to services.yml
2.In that services.yml file (around line 39) is a parameter called twig.config. Change the debug variable to true, auto_reload to true, and the cache variable to false.
3. Save and clear cache. You should now be able to use your browser’s inspector to tool to see all the possible template suggestions in the code as well as which templates are being used.
Filters in Twig.
Variables can be modified by filters. Filters are separated from the variable by a pipe symbol (|) and may have optional arguments in parentheses. Multiple filters can be chained. The output of one filter is applied to the next.
1.Join - The join filter returns a string which is the concatenation of the items of a sequence.
2.striptags - The striptags filter strips SGML/XML tags and replace adjacent whitespace by one space.
3.Trim - Removes whitspace from the string
4.Keys - Returns the array keys from the given array.
In this post i may have covered some of the basics in twig in drual 8. To get more detailed knowledge go through the this link Twig Documentation
Comments