Skip to content

Commit 5c0b71e

Browse files
authored
feat: support subexpressions in partials arguments (#140)
1 parent f17f85b commit 5c0b71e

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,9 @@ console.log(result); // --> "Users: John Doe and Alice Smith"
732732

733733
### Subexpressions
734734

735-
> Added in `v0.30.0`. Supported in helpers in `v0.37.0`.
735+
> Added in `v0.30.0`. Supported in helpers and partials in `v0.37.0`.
736736
737-
Subexpressions allow you to evaluate a function call inside another function call or helper. They are written using parentheses, and can be used anywhere a normal function argument is allowed. Example:
737+
Subexpressions allow you to evaluate a function call inside another function call, helpers, or partials. They are written using parentheses, and can be used anywhere a normal function argument is allowed. Example:
738738

739739
```hbs
740740
{{=sum (sum 3 4) 3}}
@@ -757,6 +757,12 @@ Subexpressions can be used in helpers also:
757757
{{#sayHello name=(concat "John" "Doe")}}{{/sayHello}}
758758
```
759759

760+
And in partials:
761+
762+
```hbs
763+
{{>heading text=(concat "Hello" "World")}}
764+
```
765+
760766
#### Nested subexpressions
761767

762768
Subexpressions can be nested to any depth:
@@ -789,7 +795,7 @@ You can reference variables or paths normally:
789795

790796
#### Limitations
791797

792-
- Subexpressions are currently supported in **functions** (`{{=...}}`) and **helpers** (`{{# ...}}`).
798+
- Subexpressions are currently supported in **functions** (`{{=...}}`), **helpers** (`{{# ...}}`), and **partials** (`{{> ...}}`).
793799
- Parentheses must be balanced; malformed expressions will throw an error.
794800

795801

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ const compile = (ctx, tokens, output, data, state, index = 0, section = "") => {
173173
}
174174
}
175175
else if (tokens[i].startsWith(">")) {
176-
const [t, args, opt] = parseArgs(tokens[i].replace(/^>{1,2}/, ""), data, state);
176+
const [t, args, opt] = parseArgs(tokens[i].replace(/^>{1,2}/, ""), data, state, ctx.functions);
177177
const j = i + 1, blockContent = []; // to store partial block content
178178
if (tokens[i].startsWith(">>")) {
179179
i = compile(ctx, tokens, blockContent, data, state, i + 1, t);

test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,30 @@ describe("templating", () => {
213213
};
214214
assert.equal(m(`{{>foo "tag1" "tag2" "tag3"}}`, {}, {partials}), "Tags: tag1,tag2,tag3,");
215215
});
216+
217+
it("should support subexpressions in positional arguments", () => {
218+
const options = {
219+
functions: {
220+
concat: params => params.args.join(" "),
221+
},
222+
partials: {
223+
foo: "Hello {{this}}!",
224+
},
225+
};
226+
assert.equal(m(`{{>foo (concat "John" "Doe")}}`, {}, options), "Hello John Doe!");
227+
});
228+
229+
it("should support subexpressions in key-value arguments", () => {
230+
const options = {
231+
functions: {
232+
concat: params => params.args.join(" "),
233+
},
234+
partials: {
235+
foo: "Hello {{this.name}}!",
236+
},
237+
};
238+
assert.equal(m(`{{>foo name=(concat "John" "Doe")}}`, {}, options), "Hello John Doe!");
239+
});
216240
});
217241

218242
describe("{{>> xyz}}", () => {
@@ -492,7 +516,7 @@ describe("templating", () => {
492516
assert.equal(m("{{#greet}}{{/greet}}", data, options), "Hello Bob!");
493517
});
494518

495-
it("shoudl support accessing helper content using the @helper variable", () => {
519+
it("should support accessing helper content using the @helper variable", () => {
496520
const options = {
497521
helpers: {
498522
foo: params => params.fn({value: "bar"}),

0 commit comments

Comments
 (0)