I'm generating plaintext emails with heavily personalized content: depending on various factors, sections or sentences need to be inserted, left out or phrased differently and placeholders need to be filled.

Currently I'm using Twig as a template engine, however the templates become very convoluted, because of the many nested if statements, include statements and the fight to use the whitespace controls properly: The nested if statements and their inner blocks sometimes can't be indented or separated because that would create unneccesary whitespace or blank lines. I wonder if there is a different way to assemble text snippets. My requirements are:

  • Assemble text snippets based on certain conditions (some dependent on each other, some not)
  • Be able to control paragraphs (i.e. blank lines) independently from the logic.

Optional features would be:

  • Placeholder replacement
  • Reflow of text to 75 char width, while keeping paragraphs
  • Collapse of too many blank lines.
  • Why not write code in your format and compile to Twig's format? Sounds like a simple parser. – bitsoflogic Mar 28 at 14:09

It sounds like you need some post processing of the rendered templates. What you are after shouldn't be too hard to accomplish with regular expressions.

Your first two requirements can be handled by your template engine. The post processing would kick in to remove the extra whitespace characters, and wrap lines of text.

By separating this logic you also make it much easier to test, since the post processing logic should not have any dependency on the template engine. You should be able to write unit tests for this behavior.

Depending on the framework you are using, you might be able to design a middleware component that does this automatically given certain mime types for the response, for instance text/plain.


I've done templating stuff a few times now and I always seem to quickly reach the limits of whatever template engine I happen to be using.

I find the general solution is MORE templates. ie. Rather than making your template more generic and hence complex, make multiple specific, simpler, templates. Move your conditional logic out of the template and into your template selection code.


It seems to me that you need a "closer to the metal" template and the "Plates" template engine comes to mind:

Plates is a native PHP template system that’s fast, easy to use and easy to extend. It’s inspired by the excellent Twig template engine and strives to bring modern template language functionality to native PHP templates. Plates is designed for developers who prefer to use native PHP templates over compiled template languages, such as Twig or Smarty.

There is no syntax to learn but you get all the benefits of a modern template engine. It's also one of the "php league" packages, so it's quite reliable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.