PHP Templating: Follow-Up Notes and Alternatives

Earlier, I discussed performing templating in WordPress with basic HEREDOC-style syntax, purely as an abstraction layer for hand-written markup — not as a general user-input system, so XSS wasn't a concern. Since then, several alternative approaches and tools have come up that are worth sharing.

Here’s a quick roundup of those follow-up options:

  • Function-based templating with require: One approach passes data to a function that then loads a template file with require, expecting global variables set just beforehand.
  • Twig with Timber for WordPress: If you want to use the Twig template engine inside WordPress, the Timber plugin is the go-to option, and it has been covered in detail elsewhere here.
  • Timber too heavy? A lighter alternative is the Sprig library, which shares a similar philosophy with less overhead.
  • Inline Vue templates: Yes, you can even use Vue inline templates for markup in a WordPress theme, which allows for component-style templating.
  • Broad overview: There have been extensive write-ups comparing templating languages (like Jade, Mustache, and Twig) and combinations of them in the WordPress context.
  • TinyButStrong: A lesser-known but actively maintained templating engine that fits the same purpose.