When talking about templating, regarding of if its WordPress templating, JavaScript templating, or templating in some other language, I think it’s helpful to use a concrete project or example to walk through how it works rather than talk about it in more esoteric terms.
If you’ve not been following along with this series and you’re just getting started with templating, I recommend reading the first two parts of the series:
From there, we’re going to break down the concept into two parts:
- understanding what templating is,
- how templating can work within WordPress.
The challenge, of course, comes in that we have “WordPress templates” and then we’re talking about using a templating engine to change up WordPress templates, how they work, how data is injected into them, and so on.
But I’ll do my best to try to clarify each part.
WordPress Templates (But Not Templates)
When it comes to talking about templating, it’s helpful to have a working definition of templating or templating engines. The truth is, templating is all done by a templating engine.
A Working Definition
A general definition of a templating engine is:
A template processor (also known as a template engine or template parser) is software designed to combine templates with a data model to produce result documents.
And although that’s accurate, it’s a little wordy or more complex than necessary, in my opinion, than necessary to explain the general concept. Maybe we can work off the following definition:
A template engine uses syntax to inject data into a document.
This is really general, and we’ll drill down into PHP momentarily, but first I wanted to give a basic, working definition, and then go from there namely because templating can be used across a variety of different languages.
Further, it’s important to recognize that a good templating engine will often provide a level of security, flexibility, and potentially even a “sandbox mode” out of which we test the code before deploying it to production.
What Does Templating Look Like?
Because of the nature of different syntax, templating is going to look a little bit different, but one popular PHP templating engine is called Twig. So a concrete example of PHP-based templates that use Twig may look something like this:
As you can see, it’s a bit of a difference from what we’re using to seeing with markup, HTML, PHP tags, and so on, although there are still tags unique to the templating engine.
Even still, if you’re familiar with the concepts of objects, you can see how you’d access them within the context of the template using dot-notation rather than whatever notation or function calls you’re used to using in a PHP document.
Are There WordPress Templating Engines?
Yes and no. I mean there are ways to use templating engines within the context of WordPress, but I can’t say there is a definitive templating for WordPress. Some of the examples of things you may want to check out (before reading the next post) are:
But there are other options.
But I’ll likely be using something like Timber for the next post.