template-literals-express
Template middleware for express.js based on vanilla es6 template literals
A light-weight template engine for express.js based on vanilla es6 template literals.
Each javascript file in a views
direcory is a module on its own. Why not? The fact that
const string = name = > `Hello !`console // "Hello world!"
brings us to the modularity:
// main_page.js const main_page = obj = > return `<html><body><h1>Hello !</h1></body></html>`moduleexports = main_page // and then in a router: app
Installation
npm install template-literals-express
Under the hood template-literals-express
uses hotreloader.js as a module hot-reloading with no need the server restart.
To enable hot-reloading please set development:true
in options. In a production stage please set to false.
app.use(render({root:'views', development: true}))
Server example
const express = ;const render = ; const app = ; // root - directory of templating files// development if true hotreloader.js will work, others will not workapp appapp
main_page.js
//main_page.js const main_page = n = >return `<html><body><h1>Hello !</h1></body></html>`;moduleexports = main_page
API
res.rendel
- is a mimic of express's res.render function. Get the compiled string and send it to the client.res.rendel('file_name',{options:variables})
res.compile
- the same asres.rendel
but returns only a compiled string. Usful for ajax calls from client side.res.compile('file_name',{options:variables})
It can be done so:
res
Options
- developmnet - true or false. By default false. If false
hotreloader.js
will not work. - root - it's a folder with templating modules.
Error handler
Via try{}catch(e){}
traps the errors and displays them to the browser.
Note
Nested directories template-literals-express
does not support.
Support only .js
extension files.
Naming convention
As it is you may need to giving the unique and the same names to your files and functions with underscore
Let's say your site have admin and users parts.
So in a views
directory just for example
`- admin_articles.js
- admin_dashboard.js
- user_dashboard.js
- README etc `
Just name the function also the same:
//admin_articles.jsconst admin_articles = {return `blah blah blah`} moduleexports = admin_articles res
Hot-reloading
hotreloader.js
does not work with destructuring assignments
As it is:
//some_module.jsconst some_var = n = > return `Hello !`moduleexports = some_var // bad: var some_var = ;//hot-reloading will not workvar s = // not so bad: var some_var = ; // hot-reloading will workvar s=some_var;console// > "Hello, Globik!"
Vanilla javascript for a based functionalities
Includes, partials like in other template engines can be achieved with javascript. One module can include other modules.
//head.jsconst head = n = > return `<meta charset="utf-8"><title></title><link rel="shortcut icon" type="image/ico" href="/images/w4.png"> `; {let s='';ncsslreturn s;} {let s='';njsreturn s;} moduleexports = head //footer.js const footer = n = > return `<b>footer content</b>`;moduleexports = footer //main_page.js const head = ;const footer = ; const main_page = n = > return `<html><head></head><body><h1>Some content.</h1><footer></footer></body></html>`;{return `h1 {background: green;}`;}moduleexports=main_page
forEach loop
`<div></div>`...{let s='<ul>';arrayreturn s;}
Vidgets like workaround
You can directly render the simply modules(via ajax requests) For example:
//vidget_hello_world.js const vidget_hello_world = n = > return `<b>Date:</b>`;moduleexports = vidget_hello_world //router.js app // on a client side the ajax post-call to '/get_date_vidget':<div id="content"></div>...xhr;xhr{ifxhrstatus == 200var data = JSON;documentinnerHTML = datacontent;console;}...
Examples
Also see a real world example
Caveats
- in memory. Should be using with hotreloader.
- no highlighting for html template literals syntax in a most well known code editors. Just one color.
- no layout support.
- no nested directories support
Benefits
- it can work in conjuction with others template engines.
- learning new syntax is not required.
- flexible