drupal|May 07, 2020|2 min read

Twig Templating - Most useful functions and operations syntax

TL;DR

Quick reference for essential Twig syntax including variable assignment, conditionals, loops, filters, and common operations used in Drupal templates.

Twig Templating - Most useful functions and operations syntax

Introduction

Twig is a powerful template engine for php. Drupal uses it heavily.

Lets look at some of the most useful methods and their syntax.

Setting some value to a variable

{% raw %}
{% set product_title = "Some value" %}
{% endraw %}

Setting some Object value to a variable

If you want to assign value to a variable from some object.

{% raw %}
{% set product_title = "#{paragraph.getTitle()}" %}
{% endraw %}

Strip html tags from rendered string

Say, I have a rendered string and I want to strip html tags from it. And, finally trim it. In this example, you will also see array usage.

{% raw %}
{% set my_code = content.field1[0] | striptags | trim %}
{% endraw %}

Dyanamic assigning value to a variable based on condition

{% raw %}
{% if content.field_usa_code[0] %}
    {% set usa_code = content.field_usa_code[0] | striptags|trim %}
{% endif %}
{% endraw %}

Concatenation of string with variables

{% raw %}
{% set link_india = "https://www.xyz.com/#{india_code}/?param1=#{india_code}" %}
{% endraw %}

Here, I’m using variables inside a string. Take a look at its alternative below.

Concatenation of string with variables

{% raw %}
{% set link_india = "https://www.xyz.com/" ~ (india_code) ~ "/?param1=" ~ (india_code)" %}
{% endraw %}

Complex example using string concatenation with variable with functions

{% raw %}
{% set amazon_link_usa = "https://www.xyz.com/?param1=" ~ (product_title | slice(0,50) | url_encode) ~ "&param2=#{usa_code}" %}
{% endraw %}

Here, I’m trimming the big string to 50 character max. Even if string is small, this code works. And its using another function url_encode to url encode a parameter to http query.

Related Posts

How to Use Google Fonts in Website design with Bootstrap

How to Use Google Fonts in Website design with Bootstrap

If you are using Bootstrap, the default font that comes with the package is…

How to build FIPS enabled Openssl in docker

How to build FIPS enabled Openssl in docker

Introduction In this post, we will see how we can build FIPS enabled openssl in…

Drupal DB Query Code to Fetch Active Users and Accessed Website Within last One Year

Drupal DB Query Code to Fetch Active Users and Accessed Website Within last One Year

Introduction Here, we will see the drupal code to fetch all the active users…

Implement a command line shell by using Command Dispatcher in Python

Implement a command line shell by using Command Dispatcher in Python

Lets implement a command shell by using a command dispatcher. The objective is…

Youtube API in NodeJs, Usage with Example

Youtube API in NodeJs, Usage with Example

This library is ES6, promise compatible. Or, in your package.json file, include…

How to take Backup from MongoDB and Restore to MongoDB

How to take Backup from MongoDB and Restore to MongoDB

This will take backup of your passed database name, to the passed folder. It…

Latest Posts

System Design Patterns for Managing Long-Running Tasks

System Design Patterns for Managing Long-Running Tasks

Introduction Some operations simply can’t finish in the time a user is willing…

System Design Patterns for Real-Time Updates at High Traffic

System Design Patterns for Real-Time Updates at High Traffic

The previous articles in this series covered scaling reads and scaling writes…

System Design Patterns for Handling Large Blobs

System Design Patterns for Handling Large Blobs

Introduction Every non-trivial application eventually needs to handle large…

Explaining SAGA Patterns with Examples

Explaining SAGA Patterns with Examples

In a monolith, placing an order is a single database transaction — deduct…

System Design Patterns for Scaling Writes

System Design Patterns for Scaling Writes

In the companion article on scaling reads, we covered caching, replicas, and…

Serverless vs Containers — The Decision I Keep Revisiting

Serverless vs Containers — The Decision I Keep Revisiting

Every time I start a new service, I have the same argument with myself. Lambda…