Now we set our sights on creating a Twig function. This is quick and easy because all of the supporting code is already there.
With our Twig filter done, let’s focus on a function. The difference between a filter and function is that a filter is about manipulating data passed into the filter and then returning it. A function, on the other hand, is for when you want to generate something, like content.
But a function can also be helpful when you need to pass in some arguments that will be used to manipulate or control output of the data.
In our case, we want to build two functions (one this video and one in the next). One will output a list of plant-based ingredients. This is our content generation example. The other will transform any text passed into it along with the replacement values passed in as an argument.
We already have our Twig extension class created and initialized in the module class file, so we can focus solely on creating a couple of functions.
Extending Twig in Craft CMS is made up of the following videos: