https://github.com/fffunction/nunjucks-capture
📝 A nunjucks port of Liquids capture tag
https://github.com/fffunction/nunjucks-capture
Last synced: 8 months ago
JSON representation
📝 A nunjucks port of Liquids capture tag
- Host: GitHub
- URL: https://github.com/fffunction/nunjucks-capture
- Owner: fffunction
- License: mit
- Created: 2015-11-20T14:50:17.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2020-06-03T09:18:15.000Z (about 6 years ago)
- Last Synced: 2024-10-30T06:32:09.051Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 224 KB
- Stars: 1
- Watchers: 3
- Forks: 2
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nunjucks-capture
> A nunjucks port of the [Liquid Capture tag](https://docs.shopify.com/themes/liquid-documentation/tags/variable-tags#capture)
[](https://travis-ci.org/fffunction/nunjucks-capture) [](https://coveralls.io/github/fffunction/nunjucks-capture?branch=master)
[](https://greenkeeper.io/)
## Install
```sh
npm install nunjucks-capture
```
## Usage
Add the extension to the Nunjucks environment:
```js
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:
```html+jinja
{% capture as="demo" -%}
Hello, world!
{% include 'includes/content.html' %}
{%- endcapture %}
{{ demo }}
{{ demo | e }}
```
will result in:
```html
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](https://mozilla.github.io/nunjucks/api.html)
- [Nunjucks templating documentation](https://mozilla.github.io/nunjucks/templating.html)
[](http://fffunction.co "Built by fffunction, with love and coffee")