Skip to content

Commit

Permalink
Add "wrapper" support
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Kolárik committed Jun 9, 2014
1 parent bd10956 commit 9a039cb
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 34 deletions.
31 changes: 16 additions & 15 deletions lib/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,22 @@ function load(file, options) {
}

return ractiveLoad(file.replace(/\\/g, '/')).then(function (Component) {
var basePath = path.relative(options.settings.views, path.dirname(file));
options.template = utils.selectTemplate(options, Component);
Component.defaults.template = options.template;

return Promise.join(utils.buildComponentsRegistry(options), utils.buildPartialsRegistry(basePath, options), function (components, partials) {
_.assign(Component.components, components);
_.assign(Component.partials, partials);
options.components = {};
options.partials = {};

if (options.cache) {
rr.cache['load!' + file] = Component;
}

return Component;
return utils.wrap(utils.selectTemplate(options, Component), options).then(function (template) {
var basePath = path.relative(options.settings.views, path.dirname(file));
Component.defaults.template = options.template = template;

return Promise.join(utils.buildComponentsRegistry(options), utils.buildPartialsRegistry(basePath, options), function (components, partials) {
_.assign(Component.components, components);
_.assign(Component.partials, partials);
options.components = {};
options.partials = {};

if (options.cache) {
rr.cache['load!' + file] = Component;
}

return Component;
});
});
});
}
2 changes: 1 addition & 1 deletion lib/ractive-render.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* ractive-render 0.1.0
* ractive-render 0.1.1
* https://github.com/MartinKolarik/ractive-render/
*
* Copyright (c) 2014 Martin Kolárik
Expand Down
25 changes: 13 additions & 12 deletions lib/rvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,22 @@ function load(file, options) {
}
});

var basePath = path.relative(options.settings.views, path.dirname(file));
options.template = utils.selectTemplate(options, Component);
Component.defaults.template = options.template;
return utils.wrap(utils.selectTemplate(options, Component), options).then(function (template) {
var basePath = path.relative(options.settings.views, path.dirname(file));
Component.defaults.template = options.template = template;

return Promise.join(utils.buildComponentsRegistry(options), utils.buildPartialsRegistry(basePath, options), function (components, partials) {
_.assign(Component.components, components);
_.assign(Component.partials, partials);
options.components = {};
options.partials = {};
return Promise.join(utils.buildComponentsRegistry(options), utils.buildPartialsRegistry(basePath, options), function (components, partials) {
_.assign(Component.components, components);
_.assign(Component.partials, partials);
options.components = {};
options.partials = {};

if (options.cache) {
rr.cache['rvc!' + file] = Component;
}
if (options.cache) {
rr.cache['rvc!' + file] = Component;
}

return Component;
return Component;
});
});
});
}
Expand Down
4 changes: 3 additions & 1 deletion lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ function load(file, options) {
}

return fs.readFileAsync(file, 'utf8').then(function (template) {
return utils.wrap(Ractive.parse(template), options);
}).then(function (template) {
var basePath = path.relative(options.settings.views, path.dirname(file));
var cOptions = utils.buildOptions(options, null, true);
cOptions.template = Ractive.parse(template);
cOptions.template = template;

return Promise.join(utils.buildComponentsRegistry(cOptions), utils.buildPartialsRegistry(basePath, cOptions), function (components, partials) {
cOptions.components = components;
Expand Down
62 changes: 61 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ utils.buildPartialsRegistry = function (basePath, options) {
*
* @param {Object} options
* @param {Object} Component
* @returns {Object}
* @returns {Array}
* @public
*/
utils.selectTemplate = function (options, Component) {
Expand All @@ -135,6 +135,24 @@ utils.selectTemplate = function (options, Component) {
return options.template;
};

/**
* Wrap the given template
*
* @param {Array} template
* @param {Object} options
* @returns {Promise}
* @public
*/
utils.wrap = function (template, options) {
if (!options.wrapper) {
return Promise.resolve(template);
}

return fs.readFileAsync(path.join(options.settings.views, options.wrapper), 'utf8').then(function (wrapper) {
return replacePartial(Ractive.parse(wrapper), 'content', template);
});
};

/**
* Return a list of all partials used in the given template
*
Expand All @@ -156,4 +174,46 @@ function findPartials(fragment) {
});

return _.uniq(found);
}

/**
* Replace partial
*
* @param {Array} template
* @param {string} partialName
* @param {Array} newContent
* @returns {Array}
* @private
*/
function replacePartial(template, partialName, newContent) {
walkRecursive(template, function (item, index, fragment) {
if (item.t === 8 && item.r === partialName) {
fragment.splice.apply(fragment, [index, 1].concat(newContent));

// break;
return false;
}
});

return template;
}

/**
* Walk recursive
*
* @param {Array} array
* @param {function} callback
* @returns {boolean}
* @private
*/
function walkRecursive(array, callback) {
for(var i = 0, c = array.length; i < c; i++) {
if (_.isObject(array[i].f)) {
if (walkRecursive(array[i].f, callback) === false) {
return false;
}
} else if (callback(array[i], i, array) === false) {
return false;
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ractive-render",
"description": "A simple way to render Ractive templates on node.js",
"version": "0.1.0",
"version": "0.1.1",
"homepage": "https://github.com/MartinKolarik/ractive-render/",
"github": "https://github.com/MartinKolarik/ractive-render/",
"main": "index.js",
Expand Down
1 change: 1 addition & 0 deletions test/samples/load/wrapper.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="my-cl">{{>content}}</div><span></span>
1 change: 1 addition & 0 deletions test/samples/rvc/wrapper.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{>content}}<div class="my-cl"></div><span></span>
1 change: 1 addition & 0 deletions test/samples/template/wrapper.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="my-cl"></div><span></span>{{>content}}
24 changes: 24 additions & 0 deletions test/tests/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,5 +549,29 @@ describe('load', function () {
});
});
});

describe('wrap', function () {
it('should render the component', function (done) {
app.render('load/template.html', { data: { name: 'Word' }, wrapper: 'load/wrapper.html' }, function (err, html) {
if (err) {
done(err);
}

expect(html).to.equal('<div class="my-cl"><p>Hello Word!</p></div><span></span>');
done();
});
});

it('should cache the correct template', function (done) {
app.render('load/template.html', { data: { name: 'Word' } }, function (err, html) {
if (err) {
done(err);
}

expect(html).to.equal('<div class="my-cl"><p>Hello Word!</p></div><span></span>');
done();
});
});
});
});
});
6 changes: 3 additions & 3 deletions test/tests/ractive-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ describe('compile', function () {
it('should compile all templates', function (done) {
ractiveRender.clearCache();
ractiveRender.compile(app.get('views')).spread(function (load, rvc, template) {
expect(load.length).to.equal(7);
expect(rvc.length).to.equal(8);
expect(template.length).to.equal(7);
expect(load.length).to.equal(8);
expect(rvc.length).to.equal(9);
expect(template.length).to.equal(8);

done();
});
Expand Down
24 changes: 24 additions & 0 deletions test/tests/rvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,5 +583,29 @@ describe('rvc', function () {
});
});
});

describe('wrap', function () {
it('should render the component', function (done) {
app.render('rvc/template.html', { data: { name: 'Word' }, wrapper: 'rvc/wrapper.html' }, function (err, html) {
if (err) {
done(err);
}

expect(html).to.equal('<p>Hello Word!</p><div class="my-cl"></div><span></span>');
done();
});
});

it('should cache the correct template', function (done) {
app.render('rvc/template.html', { data: { name: 'Word' } }, function (err, html) {
if (err) {
done(err);
}

expect(html).to.equal('<p>Hello Word!</p><div class="my-cl"></div><span></span>');
done();
});
});
});
});
});
24 changes: 24 additions & 0 deletions test/tests/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,5 +420,29 @@ describe('template', function () {
});
});
});

describe('wrap', function () {
it('should render the template', function (done) {
app.render('template/template.html', { data: { name: 'Word' }, wrapper: 'template/wrapper.html' }, function (err, html) {
if (err) {
done(err);
}

expect(html).to.equal('<div class="my-cl"></div><span></span><p>Hello Word!</p>');
done();
});
});

it('should cache the correct template', function (done) {
app.render('template/template.html', { data: { name: 'Word' } }, function (err, html) {
if (err) {
done(err);
}

expect(html).to.equal('<div class="my-cl"></div><span></span><p>Hello Word!</p>');
done();
});
});
});
});
});

0 comments on commit 9a039cb

Please sign in to comment.