koa-rend
A light-weight template engine for Koa.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: router
Works good with kao.js v1, v2. More flexibility, more custom behavor.
Low level api, it's all up to you.
Installation
npm install koa-rend
Under the hood koa-rend uses hotreloader.js as a hot-reloading solution with no need the server restart.
To enable hot-reloading please set development:true
in options. In a production stage please set to false.
Server example
const Koa = ;const Router=;const render = ; const app = ;const router = ;// root - directory of templating files// development if true hotreloader.js will work, others will not work app router appapp
main_page.js
//main_page.js const main_page = n = >return `<html><body><h1>Hello !</h1>// koa-passport.js is using ctx.state.user instance available from everywhere<p>Hi user! Your name is </p><p><b>Data:</b></p><p><b>Your name to upper case:</b></p></body></html>`;moduleexports = main_page
Options
- root - it's a root directory where are our templating javascript files.
views
or any other. - development - true or false. hotreloader.js would work if true. Default false.
Under the hood koa-render uses
hotreloader.js
for hot-reloading with no need server to restart.hoteloader.js
listen to the directory of root folder. Nested directories koa-rend 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 await ctx
Hot-reloading
In order to hot-reloading the files after some modifications you may need to set a development field to true. Hot-reload 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 router // 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
- no highlighting for html template literals syntax in a most well known code editors. Just one color.
- some times hot reloading works not correctly. It's a bug. One need rechange the file.
- no layout support.