mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-26 13:45:15 -07:00
express
This commit is contained in:
@@ -39,7 +39,7 @@ engine.render(tpl, {name: 'alice'}); // Alice
|
|||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var engine = Liquid({
|
var engine = Liquid({
|
||||||
root: path.resolve(__dirname, 'views/'),
|
root: path.resolve(__dirname, 'views/'), // for layouts and partials
|
||||||
extname: '.liquid',
|
extname: '.liquid',
|
||||||
cache: false
|
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 `""`.
|
`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
|
## Includes
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ const Tag = require('./src/tag.js');
|
|||||||
const Filter = require('./src/filter.js');
|
const Filter = require('./src/filter.js');
|
||||||
const Template = require('./src/parser');
|
const Template = require('./src/parser');
|
||||||
const Expression = require('./src/expression.js');
|
const Expression = require('./src/expression.js');
|
||||||
const tagsPath = path.join(__dirname, "tags/");
|
const tags = require('./tags');
|
||||||
const filtersPath = path.join(__dirname, "filters.js");
|
const filters = require('./filters');
|
||||||
|
|
||||||
var _engine = {
|
var _engine = {
|
||||||
init: function(tag, filter, options) {
|
init: function(tag, filter, options) {
|
||||||
@@ -23,6 +23,10 @@ var _engine = {
|
|||||||
this.filter = filter;
|
this.filter = filter;
|
||||||
this.parser = Template(tag, filter);
|
this.parser = Template(tag, filter);
|
||||||
this.renderer = Render();
|
this.renderer = Render();
|
||||||
|
|
||||||
|
tags(this);
|
||||||
|
filters(this);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
parse: function(html) {
|
parse: function(html) {
|
||||||
@@ -54,12 +58,22 @@ var _engine = {
|
|||||||
handleCache: function(filepath) {
|
handleCache: function(filepath) {
|
||||||
assert(filepath, 'filepath cannot be null');
|
assert(filepath, 'filepath cannot be null');
|
||||||
filepath = path.resolve(this.options.root, filepath);
|
filepath = path.resolve(this.options.root, filepath);
|
||||||
if(path.extname(filepath) === ''){
|
if (path.extname(filepath) === '') {
|
||||||
filepath += this.options.extname;
|
filepath += this.options.extname;
|
||||||
}
|
}
|
||||||
var tpl = this.options.cache && this.cache[filepath] ||
|
var tpl = this.options.cache && this.cache[filepath] ||
|
||||||
this.parse(fs.readFileSync(filepath, 'utf8'));
|
this.parse(fs.readFileSync(filepath, 'utf8'));
|
||||||
return this.options.cache ? (this.cache[filepath] = tpl) : tpl;
|
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);
|
var engine = Object.create(_engine);
|
||||||
|
|
||||||
engine.init(Tag(), Filter(), options);
|
engine.init(Tag(), Filter(), options);
|
||||||
registerTagsAndFilters(engine);
|
|
||||||
return 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.lexical = lexical;
|
||||||
factory.isTruthy = Expression.isTruthy;
|
factory.isTruthy = Expression.isTruthy;
|
||||||
factory.isFalsy = Expression.isFalsy;
|
factory.isFalsy = Expression.isFalsy;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
};
|
||||||
@@ -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
@@ -23,7 +23,7 @@ describe('liquid', function() {
|
|||||||
'/root/files/name.html': 'My name is {{name}}.'
|
'/root/files/name.html': 'My name is {{name}}.'
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
afterEach(function(){
|
afterEach(function() {
|
||||||
mock.restore();
|
mock.restore();
|
||||||
});
|
});
|
||||||
it('should output object', function() {
|
it('should output object', function() {
|
||||||
@@ -33,10 +33,10 @@ describe('liquid', function() {
|
|||||||
engine.parseAndRender('{{arr}}', ctx).should.equal('[-2,"a"]');
|
engine.parseAndRender('{{arr}}', ctx).should.equal('[-2,"a"]');
|
||||||
});
|
});
|
||||||
it('should parse html', function() {
|
it('should parse html', function() {
|
||||||
(function(){
|
(function() {
|
||||||
engine.parse('{{obj}}');
|
engine.parse('{{obj}}');
|
||||||
}).should.not.throw();
|
}).should.not.throw();
|
||||||
(function(){
|
(function() {
|
||||||
engine.parse('<html><head>{{obj}}</head></html>');
|
engine.parse('<html><head>{{obj}}</head></html>');
|
||||||
}).should.not.throw();
|
}).should.not.throw();
|
||||||
});
|
});
|
||||||
@@ -49,38 +49,51 @@ describe('liquid', function() {
|
|||||||
var template = engine.parse('<p>{{arr | join: "_"}}</p>');
|
var template = engine.parse('<p>{{arr | join: "_"}}</p>');
|
||||||
engine.render(template, ctx).should.equal('<p>-2_a</p>');
|
engine.render(template, ctx).should.equal('<p>-2_a</p>');
|
||||||
});
|
});
|
||||||
it('should render file', function(){
|
describe('#renderFile()', function(){
|
||||||
engine.renderFile('/root/files/foo.html', ctx).should.equal('foo');
|
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 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 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 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'
|
|
||||||
});
|
});
|
||||||
engine.renderFile('files/foo', ctx).should.equal('bar');
|
|
||||||
});
|
});
|
||||||
it('should respect cache option', function(){
|
describe('#express()', function() {
|
||||||
mock.restore();
|
it('should render templates', function() {
|
||||||
engine = Liquid({
|
engine.express()('/root/files/name.html', ctx, function(err, html) {
|
||||||
root: '/root/',
|
expect(err).to.equal(null);
|
||||||
extname: '.html',
|
expect(html).to.equal('My name is harttle.');
|
||||||
cache: true
|
});
|
||||||
});
|
});
|
||||||
mock({
|
it('should pass error when file not found', function() {
|
||||||
'/root/files/foo.html': 'foo'
|
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({
|
describe('cache', function() {
|
||||||
'/root/files/foo.html': 'bar'
|
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');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user