nunjucks-capture
A nunjucks port of the Liquid Capture tag
Install
npm install nunjucks-capture
Usage
Add the extension to the Nunjucks environment:
var nunjucks = ;var CaptureTag = ; var env = ;env;
Capture some content as a string:
{% capture as="demo" -%} <h2>Hello, world!</h2> {% include 'includes/content.html' %}{%- endcapture %} {{ demo }} <pre> {{ demo | e }}</pre>
will result in:
Hello, world!This is the included content <h2>Hello, world!</h2> <p>This is the included content</p>
Everything between the two tags is stored in a new variable as a string. Dynamic content, such as includes or loops, are evaluated before the variable is stored. This means you've captured the resulting content, not the templating.
See also: - Nunjucks API documentation - Nunjucks templating documentation