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:

1IntroductionWhat we'll learn in this course. 01:23
2What is Control Flow?Before we learn the techniques, let's first learn what Control Flow is. 03:27
3Two Types of Control FlowThe two types of Control Flow in Twig. 00:45
4Choice - IfIn Twig, we can use If to handle Choice control flow. 09:26
5Choice - Available Operators in TwigWhat operators can we use in our Twig conditionals? 03:16
6Choice - Containment OperatorWe look at how we can use the containment operator in Twig to check if a variable contains what we're looking for. 03:04
7Choice - Test OperatorWe use the Test Operator to check if a variable is what we think it is. 13:12
8Choice - switchThe switch statement in Twig is only available in the Craft CMS. It's not part of core Twig. 07:05
9Choice - Ternary OperatorWhen 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
10LoopsWe jump into the second type of Control Flow in Twig: Loops. 06:26
11Loops - Adding a ConditionalWe can make our loops a little smarter by adding a conditional to the statement. 01:32
12Loops - Simulating breakTwig 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
13Loops - Using break and continue pluginbreak 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
14Wrapping UpWe wrap up the course by reviewing what we covered and a few words of encouragement! 00:57