Thursday | 21 NOV 2024
[ previous ]
[ next ]

Thoughts on EJS

Title:
Date: 2022-01-05
Tags:  

EJS is a templating language for node. It let's you stick in straight javascript into html files and render them out. It's pretty powerful and it maps extraordinarily well to the way I think about templating. I've used a few other templating languages but EJS is the one I stick with now.

Some quality of life changes I made was changing the delimiter, fixing the indentation and adding snippets. With these changes, ejs is fantastic and as of now I have nothing that I think is a draw back. I guess the removal of the ejs syntax and embedding straight javascript would be nice but that seems impossible.

Changing the Delimiter

The first thing I did was change the delimiter from <% %> to . The reason for this is because the default delimter requires using two hands to type it out whereas the one I set lets me do the delimiter with just one hand.

I thought I would have to hack my delimiter change but thankfully it is fully customizable.

To change the delimeter, you just need to add an option once ejs is set up in the app.

app.set('view engine', 'ejs');
app.set('view options', {
     delimiter: '?',
});

Now the new delimiter will be used.

Fixing Indentation

Once this changes, my indentation got screwed up because the plugin I use defaults to using "<%%>". I use the jst plugin to do ejs highlighting and indenting, which comes with vim-polygot.

To get the indentation to be done properly, update the /.vim/bundle/vim-polygot/indent/jst.vim file and replace the % with a ?.

This should be in the GetJstIndent function. Once this is changed, now we should be able to auto indent ejs files and get things indented properly in the updated delimiter.

EJS Snippets

I use Coc and UltiSnips to set up snippets which is especially helpful for ejs. Inside an ejs file do :UltiSnipsEdit to bring up the snippets file and you can add all sorts of snippets easily.

snippet if "ejs if statement" b
<? if () { ?>
<? } else { ?>
<? } ?>
endsnippet

This snippet will generate an ejs if statement when I type in the if statement. I don't have too many snippets set up but this and a for loop snippet are very useful.

These 3 things make working with ejs a pleasure and so far it is my favourite templating engine.