Comparing version 0.0.4 to 0.0.5
@@ -12,14 +12,16 @@ 'use strict'; | ||
// TODO Support mustache templating style | ||
// http://underscorejs.org/#template | ||
/* | ||
* Compile template with | ||
* http://underscorejs.org/#template | ||
*/ | ||
exports.compile = function(path, data, disableInclude) | ||
{ | ||
var str; | ||
var buffer; | ||
try | ||
{ | ||
str = fs.readFileSync(path, {encoding:"UTF-8"}); | ||
buffer = fs.readFileSync(path, {encoding:"UTF-8"}); | ||
} | ||
catch (e) | ||
{ | ||
var msg = "File not found:: " + path; | ||
var msg = "File is not found:: " + path; | ||
console.warn(msg); | ||
@@ -29,7 +31,7 @@ return msg; | ||
var t; | ||
var t, tmp, out; | ||
try | ||
{ | ||
t = disableInclude ? str : include(str, data); | ||
t = template(t, data); | ||
tmp = disableInclude ? buffer : include(buffer, data); | ||
t = template(tmp, data); | ||
return _.template(t)(data); | ||
@@ -52,3 +54,3 @@ } | ||
* | ||
* <%@ include file="chunk"%> | ||
* <%@ include file="chunk" attr1="value1" attr2="value2" %> | ||
* | ||
@@ -60,10 +62,27 @@ * Support replaceAll should be support by /g | ||
{ | ||
return s.replace( | ||
/<jspl\s*:\s*include\s*file=['|"|](.*?)['|"|]\s*\/\s*>/g, | ||
function(match, viewName) | ||
return s.replace(/<jspl\s*:\s*include\s*(.*?)\s*[/]*>/g, | ||
function(match, content) | ||
{ | ||
var path = exports.express.get('views') + '/' + viewName + EXT; | ||
return exports.compile(path, data, true); | ||
} | ||
); | ||
var viewName; | ||
// Read key/value pair | ||
var regex = /\b(\w+)\b=['|"|](.*?)['|"|]\s*/g; | ||
while (match = regex.exec(content)) | ||
{ | ||
if(match[1] == 'file') | ||
{ | ||
viewName = match[2]; | ||
} | ||
else | ||
{ | ||
data[match[1]] = match[2]; | ||
} | ||
} | ||
if(!viewName) | ||
throw 'attribute file is missing'; | ||
var path = exports.express.get('views') + '/' + viewName + EXT; | ||
return exports.compile(path, data, true); | ||
} | ||
); | ||
}; | ||
@@ -99,3 +118,2 @@ exports.include = include; | ||
/* | ||
@@ -121,2 +139,3 @@ * Bind express application | ||
} | ||
if (typeof fn === 'function') | ||
@@ -139,2 +158,3 @@ { | ||
// Cache disabled | ||
if(!options.cache) | ||
@@ -145,2 +165,3 @@ { | ||
// Cache enabled | ||
if(!exports.cache[path]) | ||
@@ -147,0 +168,0 @@ { |
{ | ||
"name": "jspl", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "JSP-Like (JSPL) is a simple template engine based on the library: underscore. It's compliant with Express. And the syntax is similar to JSP.", | ||
@@ -5,0 +5,0 @@ "main": "lib/jspl", |
@@ -12,5 +12,6 @@ JSPL [![Build Status](https://travis-ci.org/jvmvik/jspl.svg?branch=master)](https://travis-ci.org/jvmvik/jspl) | ||
* Include HTML fragment | ||
* Web component capability | ||
* Variables: array, string | ||
* For loop | ||
* Dynamic evaluate expression | ||
* Dynamic expression evaluation | ||
@@ -141,4 +142,4 @@ see: Example section | ||
- ```<jspl: />``` : Indicate a tag | ||
- template : Tag name | ||
- file : Tag parameter | ||
- template : Name of the tag | ||
- file : Template file location | ||
@@ -162,10 +163,30 @@ For example: | ||
``` | ||
<jspl:include file="viewName" /> | ||
<jspl:include file="chunk" attr1="value1" attr2="value2" /> | ||
``` | ||
- viewName : Location of the included view without extension. The extension .html will be added automatically. | ||
- viewName : Location of the included view without extension. The extension .html will be added automatically. | ||
- ```<jspl: />``` : Indicate a tag | ||
- include : Tag name | ||
- file : Tag parameter | ||
- include : Tag name | ||
- file : File name (mandatory) | ||
- parameters : Optional parameters. | ||
The file chunk is located under the view directory. | ||
Example: | ||
views/index.html | ||
``` | ||
<jspl:include file="web_component" planet="venus" system="solar"/></html> | ||
``` | ||
views/web_component.html | ||
``` | ||
<h1><%= planet %></h1><h2><%= system %></h2> | ||
``` | ||
Output | ||
``` | ||
<html><h1>venus</h1>\n<h2>solar</h2>\n</html> | ||
``` | ||
Testing | ||
@@ -172,0 +193,0 @@ --- |
@@ -54,3 +54,2 @@ var expect = require('expect'); | ||
it('variables && include && loop', function() | ||
@@ -61,2 +60,8 @@ { | ||
}); | ||
it('web component', function() | ||
{ | ||
var s = jspl.include('<html><jspl:include file="web_component" planet="venus" system="solar"/></html>',{}); | ||
expect(s).toBe('<html><h1>venus</h1>\n<h2>solar</h2>\n</html>'); | ||
}); | ||
@@ -103,9 +108,8 @@ }); | ||
xit('Variables && include', function(done) | ||
it('Include takes variable in context', function() | ||
{ | ||
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>'); | ||
var s = jspl.render('test/data/render.html', {title: "Header 1", name: 'Mike'}); | ||
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
214
28085
28
299