Template engines
Last updated
Was this helpful?
Last updated
Was this helpful?
Out of the box, WordPress does not include any template engine integration. Most experienced developers manually integrate their preferred template engine while developing a custom WordPress theme.
However, because WordPress does not have native Composer support, fully safe integration is challenging, as some plugins may also include a template engine. This can lead to conflicts due to re-definition. In practice, this means that after manually integrating a template engine, developers must carefully choose and test the plugins they will use.
For many developers coming from the old-school WordPress theme templates approach, the advantages of using any template engine may not be immediately apparent.
First and foremost, it's important to understand that both Twig and Blade are built on top of PHP, so it's essentially a PHP tool. When comparing the engines and PHP directly, it may seem that the engines have fewer capabilities than PHP. However, this is actually one of its strengths. Using any engine for templates is often better than the native PHP approach because it:
One of the most significant issues with classic PHP templates in WordPress is the tendency to mix logic and presentation within the same template file. If you examine templates in many traditional WP themes, you'll find that they contain not only presentation markup but also direct data retrieval and processing. This results in spaghetti code that is prone to bugs and challenging to maintain.
Template engine was specifically designed for generating presentation templates. It confines you to the task of output generation and prevents you from mixing it with other tasks like data retrieval and processing.
In WordPress, to manually escape variables when generating markup to prevent cross-site scripting (XSS) vulnerabilities. With the template engine, you don't need to worry about escaping variables anymore. The template engine automatically escapes all variables passed into the template by default.
If you have a specific case where you want to pass HTML content without escaping, you can do so by using the |raw
filter in Twig, or {!! !!}
brackets in Blade, making it easy to handle both escaped and unescaped content safely.
Using engine's automatic auto-escaping feature can help improve the security of your templates by reducing the risk of XSS attacks and making your code more maintainable.
Even when using PHP templates exclusively for output generation, employing a template engine like Twig is still a preferred choice because it results in cleaner code. To illustrate, consider the examples below:
In PHP:
In Twig:
Even in this small example, it's evident that Twig leads to more readable code. Once you become accustomed to Twig, looking at real-life PHP templates with numerous variable injections will make PHP's verbosity, which necessitates opening and closing tags and using 'echo' repeatedly, seem cumbersome.
Wide usage is a bonus that can motivate you to learn Twig or Blade if you're unfamiliar with them yet.
So you can level up your skills by learning any of these amazing template systems.
If you're new to template engines, below we provide an overview of the key features to help you choose the best one for your project.
Minimum PHP version
7.4 (as WordPress)
8.2.0
Shortcuts for if
, for
and echo
statements
yes
yes
Auto-escaped output
yes
yes
Support of custom PHP functions
partials (functions and filters must be registered in Twig)
full (any PHP function can be called out-of-the-box)
Built-in set of helper functions
yes
no (native PHP should be used)
Advanced Views is a framework built using a modular approach, which means that each View or Card is independent and has its own 'Template Engine' field.
This allows you to use one View with Twig while another uses Blade. However, in practice, this is mostly useful for testing purposes, and you will likely use one engine for all Views and Cards.
Twig is used in many well-known software, like , and CMS. And even though it isn't employed in WordPress out-of-the-box, it's used in .
Meanwhile, Blade is 's templating engine, and thanks to its syntax, it's very friendly to any experienced PHP developers.