mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
tag:capture
This commit is contained in:
@@ -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]
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
};
|
||||
@@ -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/);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user