diff --git a/README.md b/README.md index 2ffcef6ee..882b8b2e3 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Documentation: - [ ] 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] diff --git a/tags/capture.js b/tags/capture.js new file mode 100644 index 000000000..948acdccd --- /dev/null +++ b/tags/capture.js @@ -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); + } + }); + +}; diff --git a/test/tags.js b/test/tags.js index b7c4aacd8..88062370c 100644 --- a/test/tags.js +++ b/test/tags.js @@ -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/); + }); });