Control Flow in Twig

In this course we will learn the fundamentals of control flow in Twig. We’ll learn what’s available to us to more carefully control how and when content is outputted in our Twig templates. Control Flow is how we tell our program (or in our case templates) when and under which circumstances to process our statements. In Twig these statements could be including a new template, setting a variable, running macro, or using a Twig output tag to output something to the screen.

Start Course
Ryan Irelan

I am the creator of CraftQuest, a web developer, and former software team manager. I spend most of my time improving CraftQuest with code and courses. When I'm not in front of the computer, I spend my time with my family, and running on the roads and trails of Austin, TX.

Control Flow in Twig is made up of the following videos:

1 Introduction What we'll learn in this course. 01:23
2 What is Control Flow? Before we learn the techniques, let's first learn what Control Flow is. 03:27
3 Two Types of Control Flow The two types of Control Flow in Twig. 00:45
4 Choice - If In Twig, we can use If to handle Choice control flow. 09:26
5 Choice - Available Operators in Twig What operators can we use in our Twig conditionals? 03:16
6 Choice - Containment Operator We look at how we can use the containment operator in Twig to check if a variable contains what we're looking for. 03:04
7 Choice - Test Operator We use the Test Operator to check if a variable is what we think it is. 13:12
8 Choice - switch The switch statement in Twig is only available in the Craft CMS. It's not part of core Twig. 07:05
9 Choice - Ternary Operator When we want to make our conditionals compact or contained within another Twig tag (like an include tag), we can use the Ternary Operator in Twig. 10:51
10 Loops We jump into the second type of Control Flow in Twig: Loops. 06:26
11 Loops - Adding a Conditional We can make our loops a little smarter by adding a conditional to the statement. 01:32
12 Loops - Simulating break Twig does not have the concept of break (or continue) while looping. But we can try to simulate it using built-in functionality. Are we successful? 06:50
13 Loops - Using break and continue plugin break and continue are not available in Twig but we can use a third party plugin in Craft to make get the functionality we seek. 07:31
14 Wrapping Up We wrap up the course by reviewing what we covered and a few words of encouragement! 00:57