Skip to main content

Twig in Drupal 8



       

                                     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

Ramesh said…
This is a nice explanation about twig template.I want to know more about drupal 8. I expecting more blogs form you are side. It's will help me to learn drupal 8.
Thanks for your views ramesh.ill surely give my best.

Popular posts from this blog

Multi site Set up on drupal on Lamp(Linux ,Apache , Mysql, Php )

Hi all, In this post , we are going to create a multisite on drupal 8 , Two  sites namely dogs and cats can share the same codebase and different database  Here am using Ubuntu 16.04  , So that some server configurations may vary depend on your server... So please follow the below steps to create a multi site on drupal 8 Step1:  Download the latest  drupal 8 project using the  link   www.drupal.org . Note: You can use any other method you follow to download drupal Project ..For Example : Console , Composer Step 2:  Extract the downloaded  package inside your Html folder For Example:  var/www/html/ drupal-8.4.3( Your downloaded folder here ) Step3:  In your Drupal Root Folder (drupal-8.4.3)       In the sites folder create the  required  sites as shown in the screenshot Here i create two sites namely cats and dogs sharing the same codebase and different database Step4: Inside the cats and dog folders   copy the default.settings file from the the defa

Things You Must Know Before Trying Forms In Drupal 8.

 You Should Invest In Forms                      What does your customer think?  What do they looking for? will decide the success of any business.    In a digital marketing, Most of the customer requirement is gathered through forms. So its   essential  to spend some  time with forms Example : a user can search a particular product using the search form on an e-commerce website. Form API in Drupal 8          As we know already Drupal use forms for most of the administrative site building. Drupal 8 follows object-oriented style of programming with the integration of symfony . Based on the purpose of the form, it can inherit the basic properties and methods of the below classes. 1.Config Base 2.FormBase 3.ConfirmBase   Note : Routing configuration is done through the yml files in drupal 8 When the user hits the valid url in the browser , Major Responses are 1.Page Content 2.Forms 3.Map..etc. Basic Routing Configuration for Form Internal path-machi