While Laravel has its own way to render emails you will later send, I find Mustache as a better solution to the design & consistency problems:

  • It doesn’t use backend code inside your text block to render dynamic content. For most PHP devs, you don’t want to mix your PHP code in your HTML files, you should be using a template engine instead. Mustache kinda’ helps you organize content without mixing your backend code with your text.
  • Simple syntax. You can do if(…), if(!…), you can iterate through arrays, both associative arrays and flat arrays, and you have a simple syntax to display content, even for nested arrays.
  • It allows you to use any text. When you’re using Laravel, you are kind of limited to design, especially when you want a fresh, custom design. If you have your own mail templates, such as those  created with MJML.io (I love to use MJML), BeeFree, or any service like that, you may need to render custom content easier. Mustache handles this for you.

From my personal experience, I use MJML to write mails’ design and I mix it up with Mustache code. When I’m rendering it to HTML, I save the files and I then just get their content when I’m sending the mails.

Yes, that’s pretty old for me, but if you like to do this in an automated way, check out  MJML repository where you can learn how to render your MJML files or you can use the  MJML API that renders it for you via a REST API.

Mustache is an engine that is able to render content based on markdown. You can assign variables and then you can later display them in your block of text, for example. We will use it to create dynamic content for your users in newsletters.

To install Mustache, open your command line or add it to your Composer dependencies:

$ composer require mustache/mustache

#mustache #dynamics #content #php #newsletter

Render Dynamic Content in Your Mails using Mustache and PHP
1.85 GEEK