1.0.4 • Published 8 years ago

nunjucks-capture v1.0.4

Weekly downloads
6
License
MIT
Repository
github
Last release
8 years ago

nunjucks-capture

A nunjucks port of the Liquid Capture tag

Build Status Coverage Status

Install

npm install nunjucks-capture

Usage

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>
    &lt;h2&gt;Hello, world!&lt;/h2&gt;
    &lt;p&gt;This is the included content&lt;/p&gt;
</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

Built by fffunction, with love and coffee