Home twitter


029

Learned about template engines, fought with writing one, found one to edit and utilize

May 18, 2020

I spent an inordinate amount of time working with a very base version of a template engine. I copied the code from the Express docs for developing a template engine with the intention of building out a full-fledged engine myself. Or at least something temporary and useful while I learned React and fully developed the front end. The replacement method the sample code offered was fairly useless, though. I am working on placing my blog posts into MongoDB as separate documents, and I wanted to use the properties from each document to fill out the dynamic details on each rendered page. It seemed to me, in theory at least, that this should be possible. In practice, however, I kept running into errors. I finally went on a search to see what I could learn about template engines and how to build one, when I came across the express-es6-template-engine. This thing has no dependencies; it is a javascript file with functions designed to identify a template literal on a template page and replace them with the paired values. This was exactly what I needed. I downloaded the file and edited it to my purposes, registered the engine with express in my source app, et voila! A functional template engine that does what I want. I am super excited about this. No more manually hard-coding html pages for my blog. I will be manually inputting the documents that house my posts to MongoDB for a bit, but even this will go by the wayside once I create the authentication system I need and build the dashboard to write posts. Little by little ...