npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-rend

v0.2.8

Published

Light-weight template engine for koa.js based on ES6 template literals

Downloads

33

Readme

koa-rend

NPM version NPM Downloads

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 ${name}!`
console.log(string("world")) // "Hello world!"

brings us to the modularity:

// main_page.js

const main_page = obj = > {
return `
<html>
<body>
<h1>Hello ${obj.name}!</h1>
</body>
</html>`
}
module.exports = {main_page}

// and then in a router:

router.get('/', async ctx = > {
ctx.body = await ctx.render('main_page', {name: 'world'})
}

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 = require('koa');
const Router=require('koa-router');
const render = require('koa-rend');

const app = new Koa();
const router = new Router();
// root - directory of templating files
// development if true hotreloader.js will work, others will not work
render(app,{root:'views', development: true})

app.use(async (ctx, next) = > {
//global variables are available via koa's ctx.state instance: 

ctx.state.time = new Date()
ctx.state.to_upper_case = string = > string.toUpperCase()
await next()
})



router.get('/', async ctx = > {
const name = 'world';

// ctx.render(file_name,{...variables})
//render the main_page.js from the views directory
ctx.body = await ctx.render('main_page', {name: name})
})

app.use(router.routes()).use(router.allowedMethods())
app.listen(5000)

main_page.js

//main_page.js

const main_page = n = >{
return `
<html>
<body>
<h1>Hello ${n.name}!</h1>
// koa-passport.js is using ctx.state.user instance available from everywhere
<p>Hi user! Your name is ${n.user ? n.user : 'a guest'}</p>
<p><b>Data:</b>${n.time}</p>
<p><b>Your name to upper case:</b>${n.to_upper_case(n.name)}</p>
</body>
</html>`;
}
module.exports = {main_page}

Options

  1. root - it's a root directory where are our templating javascript files. views or any other.
  2. 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.js
const admin_articles = n => {return `blah blah blah`}

module.exports = {admin_articles}

await ctx.render('admin_articles', {})

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.js
const some_var = n = > { return `Hello ${n.name}!`}
module.exports = {some_var}

// bad:

var {some_var} = require('some_module.js');//hot-reloading will not work
var s = some_var({name:"Globik"})

// not so bad:

var some_var = require('some_module.js'); // hot-reloading will work
var s=some_var.some_var({name:"Globik"});
console.log(s)// > "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.js
const head = n = > {
return `
<meta charset="utf-8">
<title>${n.title ? n.title : "Simple title"}</title>
<link rel="shortcut icon" type="image/ico" href="/images/w4.png"> 

${n.cssl ? get_cssl(n) : ''}
${n.csshelper ? `<style>${n.csshelper}</style>`:``}
${n.js ? get_js(n):''}
`;
}

function get_cssl(n){
let s='';
n.cssl.forEach((el,i)=>{
s+=`<link href="${el}" rel="stylesheet">`;
})
return s;
}

function get_js(n){
let s='';
n.js.forEach((el,i)=>{
s+=`<script src="${el}"></script>`;
})
return s;
}

module.exports = {head}

//footer.js

const footer = n = > { return `<b>footer content</b>`;}
module.exports = {footer}

//main_page.js

const head = require('./head.js');
const footer = require('./footer.js');

const main_page = n = > {
return `
<html>
<head>${head.head({cssl:['/css/css_1.css', '/css/css_2.css'], csshelper: `${get_style()}`, js:['/js/js_1.js']})}</head>
<body>
<h1>Some content.</h1>
<footer>${footer.footer({})}</footer>
</body>
</html>
`;
}
function get_style(){
return 	`
h1 {background: green;}
`;
}
module.exports={main_page}

forEach loop


`<div>${n.posts ? get_list(n.posts) : ''}</div>`
....
function get_list(array){
let s='<ul>';
array.forEach((el, i)=>{
s+=`<li>${el.post_title}<li>${el.post_author}<li>${el.post_body}`
})
return 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>${n.date}`;
}
module.exports = {vidget_hello_world}

//router.js

router.post('/get_date_vidget', async ctx = > {
var date = new Date();
ctx.body = {info: "OK", content: await ctx.render('vidget_hello_world',{date: date})}
})

// on a client side the ajax post-call to '/get_date_vidget':
<div id="content"></div>
...
xhr.open('post', '/get_date_vidget');
xhr.onload = function(ev){
if(xhr.status == 200){
var data = JSON.parse(this.response);
document.getElementById('content').innerHTML = data.content;
console.log(data.info);
}}
...

Examples

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.