DEV Community

Cover image for Creating an advanced Structure for HTML Email Template
Arthur Tkachenko
Arthur Tkachenko

Posted on • Updated on

Creating an advanced Structure for HTML Email Template

I was rebuilding a part of our markdown-to-email repository. I was not happy with how we handle templates in our generator.

While there are not many things that changed behind how it works, I still want to showcase them in my article.

All developers work with strings, generating chunks of HTML code. I spend days thinking about how to improve it for our benefit.

While this version of the template will be obsolete when we upgrade our generator, there is still room to forget some knowledge and learn how to keep things simple and not be seduced by ideas to complete it quickly.

So we have been working on rendering HTML for a while right now.

Our HTML template contains an old HTML4 style markup that is very sensitive and requires careful work and attention to small details. I have been asking for help from different developers, but everybody struggles with it from the beginning.

So we are combining that HTML page from small parts, and for the last nine months, I moved those parts away into a small js module.

With the help of RollupJS, it's easy to work.

Image description

I was able to extend it and even clone it. I decided to build a react-dom tree at some point instead of HTML.

So I copy-pasted everything and replaced my literals with react tags. It looks like “custom XML”, which can be just another way of presenting the information.

After that, this module didn't evolve a lot. Everything was working ok, but not ideally. I tried to expand it and polish our functionality, but things didn't move fast.

This module was located in folder "packages" with other modules that I have.

Image description

At this point, I decided that there was a lot of coding, and I was sick of it. So I created a new repository and moved it there. While it was just a copy-paste - it helped me to think differently. I was able to focus only on those 25 files.

From the beginning, we think about our template in 2 only forms: "content only" and "full template". Most bugs was in the "content" part, while the main template was pretty stable. But it was hard to go from "content only" into "full template" sometimes - we don't have an easy way to preview it or debug.

So I separated our 25 files into two parts: literals related to "not content" was named "outer template."

Image description

And I decided to replace my literals with functions.

Image description

Functions look similar to simple react components.

Image description

It gives me the ability to debug my strings how I want it.

Image description

I even created a folder with basic tests inside.

Image description

Hope you like it!

More articles about building markdown to email:

UPD. I forget to add a link to a repository with a source code.
Current code is here, but I probably rename this repository later.

Top comments (2)

Collapse
 
marcinwosinek profile image
Marcin Wosinek

Nice write up Arthur! One thing - if I were trying to use your code shown in this article, having it on screenshots instead of normal code blocks would be frustrating. But I guess one can still find it in the repo if they really need it.

Collapse
 
atherdon profile image
Arthur Tkachenko

Hi Marcin, I agree with you about the code.
Main reason why i added images - it makes this article more visual.
Plus, code is pretty simple. If newbie will read this article - i think it's better to type it, rather than just copy-paste it.

Good point about repository. I dont think i put a correct link into this article - thank you!