diff --git a/README.md b/README.md index 64de827..5f08bbe 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ JavaScript template engine. ```js var JUST = require('just'); -var just = new JUST({ root : __dirname + '/view' }); +var just = new JUST({ root : __dirname + '/view', useCache : true, ext : '.html' }); just.render('page', { title: 'Hello, World!' }, function(error, html) { console.log(error); @@ -37,7 +37,7 @@ var JUST = require('just'); var just = new JUST({ root : { layout: '<%= title %><%*%>', page: '<%! layout %>

Page content

' - } + }, useCache : true }); just.render('page', { title: 'Hello, World!' }, function(error, html) { @@ -148,9 +148,9 @@ Blocks supports more than one level of inheritance and may be redefined. ## Options - `root` Templates root folder or JavaScript object containing templates - - `ext` Extension of templates, defaulting to '.html' (not used for JavaScript objects as root) - - `useCache` Compiled functions are cached, defaulting to true - - `watchForChanges` Automatic reloading of changed templates, defaulting to false (useful for debugging, not supported for client-side) + - `ext` Extension of templates, defaulting to '' (not used for JavaScript objects as root) + - `useCache` Compiled functions are cached, defaulting to false + - `watchForChanges` Automatic reloading of changed templates, defaulting to false (useful for debugging with enabled useCache, not supported for client-side) - `open` Open tag, defaulting to '<%' - `close` Closing tag, defaulting to '%>' @@ -159,7 +159,7 @@ Blocks supports more than one level of inheritance and may be redefined. Basically, include [just.min.js](https://github.com/baryshev/just/tree/master/just.min.js) to a page and JUST ready to use. ```js -var just = new JUST({ root : '/view' }); +var just = new JUST({ root : '/view', useCache : true }); just.render('page', { title: 'Hello, World!' }, function(error, html) { console.log(error); diff --git a/examples/example.js b/examples/example.js index c058f6a..9627006 100644 --- a/examples/example.js +++ b/examples/example.js @@ -1,6 +1,6 @@ var JUST = require('./../index'); -var just = new JUST({ root : __dirname + '/view' }); +var just = new JUST({ root : __dirname + '/view', useCache : true, ext : '.html' }); just.render('page', { title: 'Hello, World!' }, function(error, html) { console.log(error); diff --git a/just.min.js b/just.min.js index e0194a5..90ab54b 100644 --- a/just.min.js +++ b/just.min.js @@ -1,5 +1,5 @@ /*! - * JUST JavaScript template engine v0.1.7 + * JUST JavaScript template engine v0.1.8 * https://github.com/baryshev/just * * Copyright 2012, Vadim M. Baryshev @@ -21,4 +21,4 @@ * Copyright Steven Levithan * Released under the MIT license */ -(function(){"use strict";var fs,path,async=function(){var a={},b=function(a,b){var c;if(a.forEach)return a.forEach(b);for(c=0;c",ext:".html",useCache:!0,watchForChanges:!1,root:"."},trimExp=/^\s+|\s+$/g,escapeExp=/([.*+?\^=!:${}()|\[\]\/\\])/g,STATE_RAW=0,STATE_PARTIAL=1,STATE_EXTEND=2,STATE_CONDITION=3,STATE_ELSE=4,STATE_SWITCH=5,STATE_CASE=6,STATE_DEFAULT=7,STATE_LOOP=8,cache={},loaders={},watchers={},regExpEscape=function(a){return String(a).replace(escapeExp,"\\$1")},parse=function(a){var b=1,c=["with (this.data) { with (this.customData) { this.buffer.push('"],d=a.split(new RegExp(regExpEscape(options.open)+"((?:.|[\r\n])+?)(?:"+regExpEscape(options.close)+"|$)")),e,f,g,h,i,j,k,l,m;for(f=0,e=d.length;fe){d.push(a.slice(e,h.index)),!cbSplit.compliantExecNpcg&&h.length>1&&h[0].replace(g,function(){var a;for(a=1;a1&&h.index=c)break}b.lastIndex===h.index&&b.lastIndex++}return e===a.length?(j||!b.test(""))&&d.push(""):d.push(a.slice(e)),d.length>c?d.slice(0,c):d},cbSplit.compliantExecNpcg=/()??/.exec("")[1]===undefined,cbSplit.nativeSplit=String.prototype.split),String.prototype.split=function(a,b){return cbSplit(this,a,b)},window.JUST=JUST,path=function(){var a=function(a,b){var c=0,d,e;for(d=a.length-1;d>=0;d--)e=a[d],e==="."?a.splice(d,1):e===".."?(a.splice(d,1),c++):c&&(a.splice(d,1),c--);if(b)while(c)a.unshift(".."),c--;return a},b=function(b){var c=b.charAt(0)==="/",d=b.slice(-1)==="/";return b=a(b.split("/").filter(function(a){return!!a}),!c).join("/"),!b&&!c&&(b="."),b&&d&&(b+="/"),(c?"/":"")+b};return{normalize:b}}();var AjaxObject=function(a,b){var c=this;this.updating=!1,this.abort=function(){c.updating&&(c.updating=!1,c.AJAX.abort(),c.AJAX=null)},this.update=function(){return c.updating?!1:(c.AJAX=null,window.XMLHttpRequest?(c.AJAX=new XMLHttpRequest,c.AJAX.overrideMimeType&&c.AJAX.overrideMimeType("text/html")):c.AJAX=new ActiveXObject("Microsoft.XMLHTTP"),c.AJAX===null?!1:(c.AJAX.onreadystatechange=function(){c.AJAX.readyState===4&&(c.updating=!1,c.callback(c.AJAX.responseText,c.AJAX.status,c.AJAX.responseXML),c.AJAX=null)},c.updating=new Date,c.AJAX.open("GET",a,!0),c.AJAX.send(null),!0))},this.callback=b||function(){}};fs=function(){var a=function(a,b,c){var d=new AjaxObject(a,function(a,b){b<200||b>399?c("Failed to load template"):c(undefined,a)});try{d.update()}catch(e){c(e)}},b=function(){};return{readFile:a,watch:b}}()}})(); \ No newline at end of file +(function(){"use strict";var fs,path,async=function(){var e={},t=function(e,t){var n;if(e.forEach)return e.forEach(t);for(n=0;n",ext:"",useCache:!1,watchForChanges:!1,root:""},trimExp=/^\s+|\s+$/g,escapeExp=/([.*+?\^=!:${}()|\[\]\/\\])/g,STATE_RAW=0,STATE_PARTIAL=1,STATE_EXTEND=2,STATE_CONDITION=3,STATE_ELSE=4,STATE_SWITCH=5,STATE_CASE=6,STATE_DEFAULT=7,STATE_LOOP=8,cache={},loaders={},watchers={},regExpEscape=function(e){return String(e).replace(escapeExp,"\\$1")},parse=function(e){var t=1,n=["with (this.data) { with (this.customData) { this.buffer.push('"],r=e.split(new RegExp(regExpEscape(options.open)+"((?:.|[\r\n])+?)(?:"+regExpEscape(options.close)+"|$)")),i,s,o,u,a,f,l,c,h;for(s=0,i=r.length;si){r.push(e.slice(i,u.index)),!cbSplit.compliantExecNpcg&&u.length>1&&u[0].replace(o,function(){var e;for(e=1;e1&&u.index=n)break}t.lastIndex===u.index&&t.lastIndex++}return i===e.length?(f||!t.test(""))&&r.push(""):r.push(e.slice(i)),r.length>n?r.slice(0,n):r},cbSplit.compliantExecNpcg=/()??/.exec("")[1]===undefined,cbSplit.nativeSplit=String.prototype.split),String.prototype.split=function(e,t){return cbSplit(this,e,t)},window.JUST=JUST,path=function(){var e=function(e,t){var n=0,r,i;for(r=e.length-1;r>=0;r--)i=e[r],i==="."?e.splice(r,1):i===".."?(e.splice(r,1),n++):n&&(e.splice(r,1),n--);if(t)while(n)e.unshift(".."),n--;return e},t=function(t){var n=t.charAt(0)==="/",r=t.slice(-1)==="/";return t=e(t.split("/").filter(function(e){return!!e}),!n).join("/"),!t&&!n&&(t="."),t&&r&&(t+="/"),(n?"/":"")+t};return{normalize:t}}();var AjaxObject=function(e,t){var n=this;this.updating=!1,this.abort=function(){n.updating&&(n.updating=!1,n.AJAX.abort(),n.AJAX=null)},this.update=function(){return n.updating?!1:(n.AJAX=null,window.XMLHttpRequest?(n.AJAX=new XMLHttpRequest,n.AJAX.overrideMimeType&&n.AJAX.overrideMimeType("text/html")):n.AJAX=new ActiveXObject("Microsoft.XMLHTTP"),n.AJAX===null?!1:(n.AJAX.onreadystatechange=function(){n.AJAX.readyState===4&&(n.updating=!1,n.callback(n.AJAX.responseText,n.AJAX.status,n.AJAX.responseXML),n.AJAX=null)},n.updating=new Date,n.AJAX.open("GET",e,!0),n.AJAX.send(null),!0))},this.callback=t||function(){}};fs=function(){var e=function(e,t,n){var r=new AjaxObject(e,function(e,t){t<200||t>399?n("Failed to load template"):n(undefined,e)});try{r.update()}catch(i){n(i)}},t=function(){};return{readFile:e,watch:t}}()}})(); \ No newline at end of file diff --git a/lib/just.js b/lib/just.js index 1519901..f839b20 100644 --- a/lib/just.js +++ b/lib/just.js @@ -1,5 +1,5 @@ /*! - * JUST JavaScript template engine v0.1.7 + * JUST JavaScript template engine v0.1.8 * https://github.com/baryshev/just * * Copyright 2012, Vadim M. Baryshev @@ -111,10 +111,10 @@ options = { open : '<%', close : '%>', - ext : '.html', - useCache : true, + ext : '', + useCache : false, watchForChanges : false, - root : '.' + root : '' }, trimExp = /^\s+|\s+$/g, escapeExp = /([.*+?\^=!:${}()|\[\]\/\\])/g, @@ -247,12 +247,12 @@ return new Function(buffer); }, loaded = function (error, file, blank) { - async.forEach(loaders[file], function (loader, callback) { + var callbacks = loaders[file]; + delete (loaders[file]); + async.forEach(callbacks, function (loader, callback) { loader(error, blank); callback(); - }, function () { - delete (loaders[file]); - }); + }, function () {}); }, read = function (file, callback) { if (Object.prototype.toString.call(options.root) === '[object Object]') { @@ -283,10 +283,10 @@ } else { try { var blank = parse(data); - loaded(undefined, file, blank); if (options.useCache) { cache[file] = blank; } + loaded(undefined, file, blank); if (options.watchForChanges) { watchers[file] = fs.watch(file, function () { watchers[file].close(); @@ -308,7 +308,7 @@ Template = function (file, data, customData) { this.file = file; if (Object.prototype.toString.call(options.root) === '[object String]') { - this.file = path.normalize(options.root + '/' + file + options.ext); + this.file = path.normalize((options.root.length ? (options.root + '/') : '') + file + options.ext); } this.data = data; this.customData = customData || {}; @@ -414,6 +414,9 @@ } }; this.render = function (template, data, callback) { + if ('function' == typeof data) { + callback = data, data = {}; + } var tpl = new Template(template, data); tpl.render(callback); }; diff --git a/package.json b/package.json index b1c5cab..fefe06e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name" : "just", - "version" : "0.1.7", + "version" : "0.1.8", "description" : "JavaScript template engine", "keywords": [ "template" ], "author" : "Vadim M. Baryshev ",