This commit is contained in:
harttle
2016-06-24 11:17:10 +08:00
parent e6cd6e4d46
commit aebf9812cb
5 changed files with 105 additions and 45 deletions
+9 -1
View File
@@ -39,7 +39,7 @@ engine.render(tpl, {name: 'alice'}); // Alice
```javascript
var engine = Liquid({
root: path.resolve(__dirname, 'views/'),
root: path.resolve(__dirname, 'views/'), // for layouts and partials
extname: '.liquid',
cache: false
});
@@ -49,6 +49,14 @@ var html = engine.renderFile("hello.html", {name: 'alice'});
`cache` default to `false`, `extname` default to `.liquid`, `root` default to `""`.
## Use with Express.js
```javascript
app.engine('liquid', engine.express());
app.set('views', './views'); // specify the views directory
app.set('view engine', 'liquid'); // register the template engine
```
## Includes
```
+17 -13
View File
@@ -10,8 +10,8 @@ const Tag = require('./src/tag.js');
const Filter = require('./src/filter.js');
const Template = require('./src/parser');
const Expression = require('./src/expression.js');
const tagsPath = path.join(__dirname, "tags/");
const filtersPath = path.join(__dirname, "filters.js");
const tags = require('./tags');
const filters = require('./filters');
var _engine = {
init: function(tag, filter, options) {
@@ -23,6 +23,10 @@ var _engine = {
this.filter = filter;
this.parser = Template(tag, filter);
this.renderer = Render();
tags(this);
filters(this);
return this;
},
parse: function(html) {
@@ -54,12 +58,22 @@ var _engine = {
handleCache: function(filepath) {
assert(filepath, 'filepath cannot be null');
filepath = path.resolve(this.options.root, filepath);
if(path.extname(filepath) === ''){
if (path.extname(filepath) === '') {
filepath += this.options.extname;
}
var tpl = this.options.cache && this.cache[filepath] ||
this.parse(fs.readFileSync(filepath, 'utf8'));
return this.options.cache ? (this.cache[filepath] = tpl) : tpl;
},
express: function() {
return (filePath, options, callback) => {
try {
var html = this.renderFile(filePath, options);
return callback(null, html);
} catch (e) {
return callback(e);
}
};
}
};
@@ -71,19 +85,9 @@ function factory(options) {
var engine = Object.create(_engine);
engine.init(Tag(), Filter(), options);
registerTagsAndFilters(engine);
return engine;
}
function registerTagsAndFilters(engine) {
fs.readdirSync(tagsPath).map(f => {
var match = /^(\w+)\.js$/.exec(f);
if (!match) return;
require("./tags/" + f)(engine);
});
require(filtersPath)(engine);
}
factory.lexical = lexical;
factory.isTruthy = Expression.isTruthy;
factory.isFalsy = Expression.isFalsy;
+19
View File
@@ -0,0 +1,19 @@
const Liquid = require('..');
module.exports = function() {
var engine = Liquid({
root: '/root/',
extname: '.html'
});
function express(filePath, options, callback) {
fs.readFile(filePath, function(err, content) {
if (err) return callback(new Error(err));
// this is an extremely simple template engine
var rendered = content.toString().replace('#title#', '<title>' + options.title + '</title>')
.replace('#message#', '<h1>' + options.message + '</h1>');
return callback(null, rendered);
});
}
return express;
};
+16
View File
@@ -0,0 +1,16 @@
module.exports = function(engine){
require("./assign.js")(engine);
require("./capture.js")(engine);
require("./case.js")(engine);
require("./comment.js")(engine);
require("./cycle.js")(engine);
require("./decrement.js")(engine);
require("./for.js")(engine);
require("./if.js")(engine);
require("./include.js")(engine);
require("./increment.js")(engine);
require("./layout.js")(engine);
require("./raw.js")(engine);
require("./tablerow.js")(engine);
require("./unless.js")(engine);
};
+44 -31
View File
@@ -23,7 +23,7 @@ describe('liquid', function() {
'/root/files/name.html': 'My name is {{name}}.'
});
});
afterEach(function(){
afterEach(function() {
mock.restore();
});
it('should output object', function() {
@@ -33,10 +33,10 @@ describe('liquid', function() {
engine.parseAndRender('{{arr}}', ctx).should.equal('[-2,"a"]');
});
it('should parse html', function() {
(function(){
(function() {
engine.parse('{{obj}}');
}).should.not.throw();
(function(){
(function() {
engine.parse('<html><head>{{obj}}</head></html>');
}).should.not.throw();
});
@@ -49,38 +49,51 @@ describe('liquid', function() {
var template = engine.parse('<p>{{arr | join: "_"}}</p>');
engine.render(template, ctx).should.equal('<p>-2_a</p>');
});
it('should render file', function(){
engine.renderFile('/root/files/foo.html', ctx).should.equal('foo');
});
it('should render file relative to root', function(){
engine.renderFile('files/foo.html', ctx).should.equal('foo');
});
it('should render file with context', function(){
engine.renderFile('/root/files/name.html', ctx).should.equal('My name is harttle.');
});
it('should render file with default extname', function() {
engine.renderFile('files/name', ctx).should.equal('My name is harttle.');
});
it('should disable cache by default', function(){
mock({
'/root/files/foo.html': 'bar'
describe('#renderFile()', function(){
it('should render file', function() {
engine.renderFile('/root/files/foo.html', ctx).should.equal('foo');
});
it('should render file relative to root', function() {
engine.renderFile('files/foo.html', ctx).should.equal('foo');
});
it('should render file with context', function() {
engine.renderFile('/root/files/name.html', ctx).should.equal('My name is harttle.');
});
it('should render file with default extname', function() {
engine.renderFile('files/name', ctx).should.equal('My name is harttle.');
});
engine.renderFile('files/foo', ctx).should.equal('bar');
});
it('should respect cache option', function(){
mock.restore();
engine = Liquid({
root: '/root/',
extname: '.html',
cache: true
describe('#express()', function() {
it('should render templates', function() {
engine.express()('/root/files/name.html', ctx, function(err, html) {
expect(err).to.equal(null);
expect(html).to.equal('My name is harttle.');
});
});
mock({
'/root/files/foo.html': 'foo'
it('should pass error when file not found', function() {
engine.express()('/root/files/name1.html', ctx, function(err, html) {
expect(err.code).to.equal('ENOENT');
});
});
engine.renderFile('files/foo', ctx).should.equal('foo');
mock({
'/root/files/foo.html': 'bar'
});
describe('cache', function() {
it('should be disabled by default', function() {
mock({
'/root/files/foo.html': 'bar'
});
engine.renderFile('files/foo', ctx).should.equal('bar');
});
it('should respect cache=true option', function() {
engine = Liquid({
root: '/root/',
extname: '.html',
cache: true
});
engine.renderFile('files/foo', ctx).should.equal('foo');
mock({
'/root/files/foo.html': 'bar'
});
engine.renderFile('files/foo', ctx).should.equal('foo');
});
engine.renderFile('files/foo', ctx).should.equal('foo');
});
});