tag:capture

This commit is contained in:
harttle
2016-06-15 22:45:50 +08:00
parent 58daab6128
commit 2498e6c202
3 changed files with 26 additions and 1 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ Documentation: <https://shopify.github.io/liquid/basics/introduction/#tags>
- [ ] tablerow [Document](https://shopify.github.io/liquid/tags/iteration/) [Source](https://github.com/harttle/shopify-liquid/blob/master/tags/.js) [Test][tt]
- [ ] tablerow [Document: cols,limit,offset,range](https://shopify.github.io/liquid/tags/iteration/) [Source](https://github.com/harttle/shopify-liquid/blob/master/tags/.js) [Test][tt]
- [x] assign [Document](https://shopify.github.io/liquid/tags/variable/) [Source](https://github.com/harttle/shopify-liquid/blob/master/tags/assign.js) [Test][tt]
- [ ] capture [Document](https://shopify.github.io/liquid/tags/variable/) [Source](https://github.com/harttle/shopify-liquid/blob/master/tags/.js) [Test][tt]
- [x] capture [Document](https://shopify.github.io/liquid/tags/variable/) [Source](https://github.com/harttle/shopify-liquid/blob/master/tags/.js) [Test][tt]
- [ ] increment [Document](https://shopify.github.io/liquid/tags/variable/) [Source](https://github.com/harttle/shopify-liquid/blob/master/tags/.js) [Test][tt]
- [ ] decrement [Document](https://shopify.github.io/liquid/tags/variable/) [Source](https://github.com/harttle/shopify-liquid/blob/master/tags/.js) [Test][tt]
+18
View File
@@ -0,0 +1,18 @@
var Liquid = require('..');
var lexical = Liquid.lexical;
var re = new RegExp(`(${lexical.identifier.source})`);
module.exports = function(liquid) {
liquid.registerTag('capture', {
needClose: true,
render: function(tokens, scope, token, hash) {
var html = liquid.renderTokens(tokens, scope);
var match = token.args.match(re);
if (!match) throw new Error(`${token.args} not valid identifier`);
scope.set(match[1], html);
}
});
};
+7
View File
@@ -44,5 +44,12 @@ describe('tags', function() {
expect(liquid.render('{% unless 1 %}yes{%else%}no{%endunless%}', ctx)).to.equal('no');
expect(liquid.render('{% unless 1>2 %}yes{%endunless%}', ctx)).to.equal('yes');
});
it('should support capture', function(){
expect(liquid.render('{% capture f %}{{"a" | capitalize}}{%endcapture%}{{f}}', ctx)).to.equal('A');
expect(function(){
liquid.render('{% capture = %}{%endcapture%}', ctx);
}).to.throw(/= not valid identifier/);
});
});