Download the PHP package oofbar/craft-twig-toolbox without Composer
On this page you can find all versions of the php package oofbar/craft-twig-toolbox. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package craft-twig-toolbox
Twig Toolbox for Craft CMS
The simplest way to keep business logic out of your Twig templates.
All this plugin does is registers a Twig extension—everything else is up to you! With Twig Toolbox, you can finally start to clean up the mess that accumulates at the top of every template…
Usage
Twig Toolbox lets you inject custom filters, functions, globals, and tests into Craft’s template engine.
To get started, install the plugin from the Craft Plugin Store, or use Composer:
Then, copy this into config/twig-toolbox.php
:
:bulb: If you’re in the mood for a challenge, writing your own Twig extension is a great way to get familiar with custom module development.
Let’s look at some examples of how each language feature can be used.
Filters
→ Documentation on Twig Filters
The filters
key should contain alphanumeric keys and functions or callables as values. Filters always have at least one argument!
Example
Functions
→ Documentation on Twig Functions
Each item in the functions
array should have an alphanumeric key, and a function as its value. The function needs to declare expected arguments, and should explicitly return a value, if appropriate.
:smile: You can use virtually any Craft API in a function!
Example:
Globals
→ Documentation on Twig Globals
Globals are best used sparingly, and only for simple values. The new custom config in Craft 4 is a near equivalent!
:warning: Be mindful of what you are assigning to a global! Calling some Craft or Plugin APIs can cause a race condition as the system initializes.
Example:
Tests
→ Documentation on Twig Tests
Tests are sort of like functions, but only available when using Twig’s is
operator. They can do a lot to make your templates read more clearly—especially when the logic behind it is convoluted.
Examples
Tips + Tricks
Callables
PHP has a special “type” for callable values. This includes the anonymous functions or “closures” we’ve used so far, in addition to a few other syntaxes that make it simple to add proxies to native PHP and Craft functions:
Handling Types
Some of the functions above could be made even more flexible by accepting the special mixed
type, or a union type. For example, the expensive
test could do some type checking and normalization like this...
...then, the template can read a bit more fluidly:
Parameterization
filters
and functions
can take arguments to customize their behavior. If you find yourself adding a number of similar helpers, take a moment to consider how they could be consolidated and parameterized with one or more arguments.
HTML Helpers
Consider how Twig can help you generate HTML, rather than trying to build it up yourself!
Help + Support
If you’re having trouble getting started, create an issue on GitHub and we’ll do our best to help out! If you need support on a project-specific task (like finding the appropriate Craft APIs), we recommend posing it to the broader community.