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

Drupal 8 - How to Theme Form and its Fields with reordering fields

Drupal 8 - How to Theme Form and its Fields with reordering fields

Introduction In this post, we will see how to theme form and its fields…

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Introduction I needed a report page, where I wanted to have some information…

Drupal 7 - Code for Exporting all your content nodes in json files

Drupal 7 - Code for Exporting all your content nodes in json files

Introduction When I migrated all of my drupal-7 website to drupal-8, I wrote…

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Introduction Assume you have a drupal website and using cloudflare. You are…

Drupal - How to rename column of a content type

Drupal - How to rename column of a content type

Introduction You already have a content type with one or more fields in it…

Drupal 8 - How to hide help link About text formats and text format guidelines

Drupal 8 - How to hide help link About text formats and text format guidelines

Problem In drupal textarea field, it was always a pain to see the two links…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…