Comparing version 0.0.1 to 0.0.2
@@ -10,7 +10,29 @@ 'use strict'; | ||
// http://underscorejs.org/#template | ||
exports.compile = function(path, data, c) | ||
exports.compile = function(path, data, disableInclude) | ||
{ | ||
var s = fs.readFileSync(path, {encoding:"UTF-8"}); | ||
var t = c ? s : include(s, data); | ||
return _.template(t, data); | ||
var str; | ||
try | ||
{ | ||
str = fs.readFileSync(path, {encoding:"UTF-8"}); | ||
} | ||
catch (e) | ||
{ | ||
var msg = "File not found:: " + path; | ||
console.warn(msg); | ||
return msg; | ||
} | ||
try | ||
{ | ||
var t = disableInclude ? str : include(str, data); | ||
var compiled = _.template(t); | ||
return compiled(data); | ||
} | ||
catch(e) | ||
{ | ||
var msg = "Parsing error:: " + e.message; | ||
console.warn(msg); | ||
console.info(e); | ||
return msg; | ||
} | ||
}; | ||
@@ -31,3 +53,3 @@ | ||
return s.replace( | ||
/<%@\s*include\s*file=['|"|](.*?)['|"|]\s*%>/g, | ||
/<%@\s+include\s+file=['|"|](.*?)['|"|]\s+%>/g, | ||
function(match, templateId) | ||
@@ -43,3 +65,3 @@ { | ||
/*** | ||
/* | ||
* Bind express application | ||
@@ -54,4 +76,8 @@ */ | ||
/* | ||
* Render template engine | ||
*/ | ||
exports.render = function(path, options, fn) | ||
{ | ||
// Run callback | ||
if ('function' == typeof options) | ||
@@ -75,10 +101,14 @@ { | ||
// Normal processing | ||
options = options || {}; | ||
if(!options.cache) | ||
{ | ||
return exports.compile(path, options); | ||
} | ||
if(exports.cache[path]) | ||
if(!exports.cache[path]) | ||
{ | ||
exports.cache[path] = exports.compile(path, options); | ||
} | ||
return exports.cache[path]; | ||
@@ -85,0 +115,0 @@ }; |
{ | ||
"name": "jspl", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "JSP-Like (JSPL) is a simple template based on underscore. Compliant with Express. Add the tag include for HTML fragment.", | ||
"main": "lib/jspl.js", | ||
"main": "lib/jspl", | ||
"scripts": { | ||
"mocha": "mocha -u bdd -R spec", | ||
"test": "mocha -R spec && npm run coverage" | ||
"test": "mocha -u bdd -R test", | ||
"coverage": "mocha -R test && npm run coverage" | ||
}, | ||
@@ -10,0 +10,0 @@ "keywords": [ |
@@ -14,3 +14,3 @@ jspl | ||
see: Gists section | ||
Getting started | ||
@@ -22,19 +22,29 @@ === | ||
2. Enable JSPL in Express 4 | ||
Edit app.js, let's JSPL bind to the express app. | ||
```Edit app.js, let's JSPL bind to the express app. | ||
var app = express(); | ||
// Disable jade by removing or commenting | ||
//app.set('view engine', 'jade'); | ||
// view engine setup | ||
app.set('views', path.join(__dirname, 'views')); | ||
// Enable | ||
app.set('view cache', false); | ||
// Bind JSPL to express | ||
jspl.bind(app); | ||
var jspl = require('jspl'); | ||
jspl.bind(app);``` | ||
Sample: sample/express/ | ||
Sample | ||
==== | ||
An example of application is available under: sample/express/ | ||
it demonstrate the integration of this simple template engine with express. | ||
Gists | ||
@@ -46,6 +56,9 @@ --- | ||
``` | ||
<h1>Hello <%= name %></h1> | ||
``` | ||
File: app.js | ||
``` | ||
app.get('/', function(req, res) | ||
@@ -55,6 +68,7 @@ { | ||
}) | ||
``` | ||
Result: | ||
<h1>Hello John</h1> | ||
```<h1>Hello John</h1>``` | ||
@@ -66,11 +80,15 @@ For loop | ||
``` | ||
<h1>Hello</h1> | ||
<ol> | ||
<% _.each(people, function(name) { %> | ||
<li><%= name %></li> | ||
<% }); %> | ||
</ol> | ||
``` | ||
File: app.js | ||
``` | ||
app.get('/', function(req, res) | ||
@@ -80,5 +98,6 @@ { | ||
}) | ||
``` | ||
Result: | ||
``` | ||
<h1>Hello</h1> | ||
@@ -91,2 +110,4 @@ <ol> | ||
``` | ||
Include fragment | ||
@@ -102,2 +123,6 @@ --- | ||
Testing | ||
--- | ||
This template engine build is animate by a complete regression test suite. | ||
Notes | ||
@@ -111,2 +136,8 @@ --- | ||
Motivation | ||
--- | ||
I do not like Jade or whatever short HTML template engine. I understand why it's a good solution for basic website. | ||
But it does not mix well with complex HTML + Javascript Framework like AngularJS. Also, I think software developer should generally minimize the number of transformation that must be done to produce an output. | ||
Roadmap | ||
@@ -116,2 +147,2 @@ --- | ||
* Speed optimization | ||
var expect = require('expect'); | ||
var jspl = require('jspl'); | ||
var jspl = require('./../lib/jspl'); | ||
describe('JSPL regression', function() | ||
describe('JSPL regression / ', function() | ||
{ | ||
@@ -22,3 +22,3 @@ beforeEach(function() | ||
describe('Include', function() | ||
describe('Include / ', function() | ||
{ | ||
@@ -41,3 +41,3 @@ it('basic include file', function() | ||
var s = jspl.include('<html><%@ include file="loop2" %></html>',{head: "Header",people:['Mike', 'Bob', 'Henry']}); | ||
expect(s).toBe('<html><h2>Header</h2>\n<ol>\n\n<li>Mike</li>\n\n<li>Bob</li>\n\n<li>Henry</li>\n\n</ol></html>'); | ||
expect(s).toBe('<html><h2>Header</h2>\n<ol>\n<li>Mike</li>\n<li>Bob</li>\n<li>Henry</li>\n</ol></html>'); | ||
}); | ||
@@ -47,3 +47,3 @@ | ||
describe('Compile', function() | ||
describe('Compile / ', function() | ||
{ | ||
@@ -53,3 +53,3 @@ it('simple', function() | ||
var s = jspl.compile('test/data/simple.html', {name: 'Victor'}); | ||
expect(s).toBe('<h1>Hello Victor</h1>\n'); | ||
expect(s).toBe('<h1>Hello Victor</h1>'); | ||
}); | ||
@@ -65,9 +65,4 @@ | ||
{ | ||
try | ||
{ | ||
var s = jspl.compile('test/data/loop.html', {}); | ||
expect(s).toBe(''); | ||
} | ||
catch(e) | ||
{expect(e.message).toBe('people is not defined');} | ||
expect(s).toBe('Parsing error:: people is not defined'); | ||
}); | ||
@@ -84,10 +79,4 @@ | ||
// Render | ||
describe('Render', function(done) | ||
describe('Render / ', function(done) | ||
{ | ||
it('Variables && include && loop', function(done) | ||
{ | ||
var json = {title: "Header 1", head: 'Header 2', people:['Mike']}; | ||
var s = jspl.render('test/data/render.html', json); | ||
expect(s).toBe('<html>\n<h1>Header 1</h1>\n<h2>Header 2</h2>\n<ol>\n\n<li>Mike</li>\n\n</ol>\n</html>'); | ||
}); | ||
@@ -97,5 +86,12 @@ it('Render a simple loop', function() | ||
var s = jspl.render('test/data/loop2.html',{title: "Header 1", head: 'Header 2', people:['Mike', 'Bob', 'Henry']}); | ||
expect(s).toBe('<h2>Header 2</h2>\n<ol>\n\n<li>Mike</li>\n\n<li>Bob</li>\n\n<li>Henry</li>\n\n</ol>'); | ||
}); | ||
expect(s).toBe('<h2>Header 2</h2>\n<ol>\n<li>Mike</li>\n<li>Bob</li>\n<li>Henry</li>\n</ol>'); | ||
}); | ||
xit('Variables && include', function(done) | ||
{ | ||
var json = {title: "Header 1", name: 'Mike'}; | ||
var s = jspl.render('test/data/render.html', json); | ||
expect(s).toBe('<html>\n<h1>Header 1</h1>\n<h1>Hello Mike</h1>\n</html>'); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
47381
42
272
139