Comparing version 0.0.4 to 0.0.5
23
jst.js
@@ -16,11 +16,24 @@ /** | ||
// filters | ||
const htmlCodes = {'&': '&', '<': '<', '>': '>', '"': '"'}, | ||
htmlre = /[&<>"]/g, | ||
htmlEscape = function (src) { return htmlCodes[src]; }; | ||
jst_filter_escape = function(src) { | ||
return typeof src !== 'string' ? src : src.replace(htmlre, htmlEscape); | ||
} | ||
// compiler | ||
var compile = exports.compile = function(ctx) { | ||
_options.useIt = ctx.indexOf('{{ it.') > -1; | ||
_options.useIt = /{{ (e\()?it\./.test(ctx); | ||
var code = (_options.useIt ? 'var out = "' : 'var out = ""; with(it) { out += "') | ||
+ ctx.replace(/[\t\r\n]/g, '') | ||
.replace(/"/g, '\\"').replace(/\{#.+?#\}/g, '') | ||
.replace(/\{\{ (.*?) \}\}/g, '"; out += $1; out += "') | ||
.split('\{% ').join('"; ') | ||
.split(' %\}').join(' out +="') | ||
.replace(/"/g, '\\"') | ||
.replace(/\{\{ (.+?) \}\}/g, '"; out += $1; out += "') | ||
.replace(/\{% (.+?) %\}/g, '"; $1 out += "') | ||
.replace(/\{#.+?#\}/g, '') | ||
.replace(/ e\(/g, ' jst_filter_escape(') | ||
+ (_options.useIt ? '"; return out;' : '"; } return out;'); | ||
@@ -27,0 +40,0 @@ return new Function('it', code.replace(' out += "";', '')); |
@@ -10,3 +10,3 @@ /** | ||
exports.version = '0.0.4'; | ||
exports.version = '0.0.5'; | ||
@@ -28,11 +28,24 @@ var _cache = {}, | ||
// filters | ||
const htmlCodes = {'&': '&', '<': '<', '>': '>', '"': '"'}, | ||
htmlre = /[&<>"]/g, | ||
htmlEscape = function (src) { return htmlCodes[src]; }; | ||
jst_filter_escape = function(src) { | ||
return typeof src !== 'string' ? src : src.replace(htmlre, htmlEscape); | ||
} | ||
// compiler | ||
var compile = exports.compile = function(ctx) { | ||
_options.useIt = ctx.indexOf('{{ it.') > -1; | ||
_options.useIt = /{{ (e\()?it\./.test(ctx); | ||
var code = (_options.useIt ? 'var out = "' : 'var out = ""; with(it) { out += "') | ||
+ ctx.replace(/[\t\r\n]/g, '') | ||
.replace(/"/g, '\\"').replace(/\{#.+?#\}/g, '') | ||
.replace(/\{\{ (.*?) \}\}/g, '"; out += $1; out += "') | ||
.split('\{% ').join('"; ') | ||
.split(' %\}').join(' out +="') | ||
.replace(/"/g, '\\"') | ||
.replace(/\{\{ (.+?) \}\}/g, '"; out += $1; out += "') | ||
.replace(/\{% (.+?) %\}/g, '"; $1 out += "') | ||
.replace(/\{#.+?#\}/g, '') | ||
.replace(/ e\(/g, ' jst_filter_escape(') | ||
+ (_options.useIt ? '"; return out;' : '"; } return out;'); | ||
@@ -39,0 +52,0 @@ return new Function('it', code.replace(' out += "";', '')); |
{ | ||
"name": "jst", | ||
"description": "Node JavaScript Template, A pretty high performance template engine", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"author": "Shaun Li <shonhen@gmail.com>", | ||
@@ -6,0 +6,0 @@ "keywords": ["template", "engine", "jst"], |
# Node JavaScript Template | ||
Node-jst is a pretty high performance template engine and implemented with JavaScript for Node.js | ||
Node-jst is a pretty high performance template engine and implemented | ||
with JavaScript for Node.js | ||
@@ -42,2 +43,5 @@ ## Installation | ||
// Filters | ||
jst.render('Hello {{ e(it.name) }}', {name: '<strong>jst</strong>'}); | ||
// Client side | ||
@@ -49,2 +53,9 @@ <script src="jst.js"></script> | ||
## Benchmarks | ||
[A brief comparison of some JavaScript templating engines on a short | ||
template: 7 DOM nodes ... 7 interpolated values.][link] | ||
[link]: http://jsperf.com/dom-vs-innerhtml-based-templating/144 | ||
## License | ||
@@ -51,0 +62,0 @@ |
var jst = require('../lib/jst'); | ||
//var jst = require('jst'); | ||
var ctx = 'Hello {{ name }}', | ||
ctx2 = 'Hello {{ it.name }}', | ||
args = {name: 'Shaun'}; | ||
var ctx = 'Hello {{ e(name) }},\n {{ gender }} bla {% if (user) { %} bla {% } %}', | ||
ctx2 = '\tHello {{ e(it.name) }},\n {{ it.gender }} bla {% if (it.user) { %} bla {% } %}', | ||
args = {name: '<h1>Shaun</h1>', gender: 'Male', user: {}}; | ||
console.log(jst.render(ctx, args)); | ||
//jst.configure({useIt: true}); | ||
console.log(jst.render(ctx2, args)); | ||
Sorry, the diff of this file is not supported yet
11847
213
83
9