Block.js : Ridiculously simple HTML templating. All it does is replaces "blocks" in your template with a local.
The crux of this module is in these simple lines of code:
1
2
3
4
5
6
7
8
9
10
function replace ( string , key , value ) {
return string . replace (
new RegExp ( '\\{\\{\\s*' + escapeRegExp ( key ) + '\\s*\\}\\}' , 'g' ),
value || ''
)
}
function escapeRegExp ( str ) {
return str . replace ( /([.*+?=^!:${}()|[\]\/\\])/g , '\\$1' )
}
The API is also pretty simple:
1
2
3
4
5
var template = Block ( html );
//html is a string, and block returns a new templating instance.
template . render ( locals );
//locals is an object of "blocks" to replace in the template.
Simple Example:
1
2
3
4
5
6
var block = require ( 'block' )
, fs = require ( 'fs' )
, tmpl = fs . readFileSync ( 'block.html' , 'utf-8' )
, data = block ( tmpl ). render ( { content : '<div class="inner"></div>' });
console . log ( data );
GIF GTW!:
Hope you liked this simple HTML templating engine! Go ahead and .replace('', string)
Thanks to the author Jonathan Ong .