Skip to content

Commit

Permalink
fix: use in for custom block existence check;
Browse files Browse the repository at this point in the history
- allows `blocks.foo: null` pattern
  • Loading branch information
lukeed committed Jul 14, 2021
1 parent 0fb1025 commit 61b6de0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/$utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function gen(input, options) {
txt += `}else if(${inner}){`;
} else if (action === 'else') {
txt += `}else{`;
} else if (extra[action]) {
} else if (action in extra) {
if (inner) {
tmp = [];
// parse arguments, `defer=true` -> `{ defer: true }`
Expand Down
61 changes: 61 additions & 0 deletions test/$index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,67 @@ compile('should create `async` function', async () => {
assert.ok(delta > 29 && delta < 35);
});

compile('should allow `blocks` to call other blocks', () => {
let blocks = {
hello(args, blocks) {
let output = `<h>"${args.name}"</h>`;
// Always invoke the `foo` block
output += blocks.foo({ value: 123 });
// Calls itself; recursive block
if (args.other) output += blocks.hello({ name: args.other }, blocks);
return output;
},
foo(args) {
return `<foo>${args.value}</foo>`;
}
};

let render = tempura.compile('{{#hello name="world" other="there"}}', { blocks });

assert.is(
render(),
`<h>"world"</h><foo>123</foo><h>"there"</h><foo>123</foo>`
);
});

compile('should allow `Compiler` output as blocks', () => {
let blocks = {
// initialize foo
// ~> does NOT use custom blocks
foo: tempura.compile(`
{{#expect age}}
{{#if age > 100}}
<p>centurion</p>
{{#else}}
<p>youngin</p>
{{/if}}
`),

// initial hello
// ~> placeholder; because self-references
hello: null,
};

blocks.hello = tempura.compile(`
{{#expect name, other}}
<h>"{{ name }}"</h>
{{#foo age=123}}
{{#if other}}
{{#hello name=other}}
{{/if}}
`, { blocks });

let normalize = x => x.replace(/[\r\n\t]+/g, '');
let render = tempura.compile('{{#hello name="world" other="there"}}', { blocks });

assert.is(
normalize(render()),
`<h>"world"</h><p>centurion</p><h>"there"</h><p>centurion</p>`
);
});

compile.run();

// ---
Expand Down

0 comments on commit 61b6de0

Please sign in to comment.