Twig basics - Drupal 8

Nowadays drupal is best most customize php framework in use. Drupal version 8 come with support of twig language and syntax.

In this example we will discuss about “twig” language which is widely used as supportive language of drupal 8. let’s start with an overview of ” twig syntax ” and “functionality“.

Twig Syntax :

Twig syntax does three things.

(a) Says something : It is represented by the {{….}}.

Here opening and closing braces are identified by the parser.

which is as something that twig needs to do.

(….)  represents a variable that will be printed on the page

for example  { { template } } would be print the value associated  with a variable called “ template “.

(b) Does something : It is used for “ if ” and “ for” statements.

we have to setting the values of the variables, filters, other common functions.

Syntax is { %…% }.

For example { % filter lower % }

   { {template} }

 { % endfilter % }

This will print the value of template in “ lowercase“.

Above (a) and (b) provides all the functionality which is required to connect content in drupal with the rendered page.

(c) It’s a comment : This syntax shows comment.

Twig Variables

Main functionality of twig starts with how to print the ” content ” of a variable on  a page.

If you have a variable called template_name

Printing the value of this variable to the page is  achieved through { {template_name } }

question is that from where template_name comes, a module that generated a value and assigned it to a theme variable named as template_name.

Drupal modules are primarly more complex such as array, an object, or function.

Twig handles automatically searching for the possible sources of that variable.

for example of a object named as “ customer ” with properties ( attributes ) as follows address, city, state, postal_code, email and phone.

Here we can print the value of customer’s name using { {customer.name} }

It searches for the following :

1 $customer[‘name’]

2 $customer ->name

3 $customer ->__isset(‘name’) && $customer->__get(‘name’)

4 $customer -> name ()

5 $customer ->getName ()

6 $customer ->isName()

7 $customer ->__call(‘name’)

firstly “ twig ” searches its nearly cases, one of the references will return the  cusomer’s name.

while most of the variable are created by the modules. but in  most of cases where  you need to create and use a variable in a template.

for creation of a variable and assign a value to it, use the following syntax

{  % set template_name  = ” Drupal  Theme ” % }

twig also support associative array which is based on the Key => value arrays

In  twig they are called as “ hashes

To create a hash, use the followiing syntax

{ % Book_name  =  { name :  ” Drupal beginner ” author : ” Rasmus cliff ” }  % }

Let’s take another example

In this example we will print the values using { { name.first } } or { { name.last } }

{% 
   set books = {
				 Php: { Author: "rasmus cliff", published: "1985" },
				 Java: { Author: "james gosling", published: "1991" },
				 C: { Author: "Dennis ritchie", published: "1976" },
				 C++: { Author: "Bjarne", published: "1989" }
				} 
%}

Note: Hashes may also be nested.

you would print the values as { { books.php.Author } }

Output: rasmus cliff