parse-blocks
v0.2.1
Published
Parse basic template blocks in a string, like Handlebars block expressions but returns an object. Delimiters are customizable.
Downloads
3
Readme
parse-blocks
Parse basic template blocks in a string, like Handlebars block expressions but returns an object. Delimiters are customizable.
Install with npm
npm i parse-blocks --save
Run tests
npm test
Usage
var blocks = require('parse-blocks');
Example
Given the following arbitrary blocks in fixtures/c.txt
:
{{#definedoc "foo" a.b}}
This is content foo
{{/definedoc}}
{{#definedoc "bar" a.c}}
This is content bar
{{/definedoc}}
{{#definedoc "baz" a.d}}
This is content baz
{{/definedoc}}
The following:
var str = fs.readFileSync('fixtures/c.txt', 'utf8');
console.log(blocks(str));
Results in:
{ definedoc:
{ foo:
{ args: [ 'a.b' ],
content: 'This is content foo',
orig:
[ '{{#definedoc "foo" a.b}}',
'This is content foo',
'{{/definedoc}}' ] },
bar:
{ args: [ 'a.c' ],
content: 'This is content bar',
orig:
[ '{{#definedoc "bar" a.c}}',
'This is content bar',
'{{/definedoc}}' ] },
baz:
{ args: [ 'a.d' ],
content: 'This is content baz',
orig:
[ '{{#definedoc "baz" a.d}}',
'This is content baz',
'{{/definedoc}}' ] } } }
Context
Context can be passed as thisArg
when three arguments are passed, and/or on options.locals
.
Example
blocks(str, {locals: {a: {name: 'AAA'}}});
And this block:
{{#apidoc "aaa" a}}
This is content <%= name %>
{{/apidoc}}
Results in the following object:
{ apidoc:
{ aaa:
{ args: [ 'a' ],
context: { name: 'AAA' },
content: 'This is content <%= name %>',
orig:
[ '{{#apidoc "aaa" a}}',
'This is content <%= name %>',
'{{/apidoc}}' ] }
The content
can now be used to pass to any template engine with context
as locals.
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Author
Jon Schlinkert
License
Copyright (c) 2014 Jon Schlinkert
Released under the MIT license
This file was generated by verb on December 02, 2014.