A nunjucks port of the Liquid Capture tag
npm install nunjucks-capture
Add the extension to the Nunjucks environment:
var nunjucks = require('nunjucks');
var CaptureTag = require('nunjucks-capture');
var env = new nunjucks.Environment();
env.addExtension('CaptureTag', new CaptureTag());
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:
<h2>Hello, world!</h2>
<p>This is the included content</p>
<pre>
<h2>Hello, world!</h2>
<p>This is the included content</p>
</pre>
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