From 9a379d3f25c040dcec549abf69969a9ed94ca8ad Mon Sep 17 00:00:00 2001 From: Josemi Juanes <5751201+jmjuanes@users.noreply.github.com> Date: Tue, 24 Dec 2024 17:12:10 +0100 Subject: [PATCH 1/3] Add support for block content in partials --- index.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ac53ffd..d286ba0 100644 --- a/index.js +++ b/index.js @@ -118,10 +118,15 @@ const create = (template = "", options = {}) => { i = i + lastIndex + 1; } else if (tokens[i].startsWith(">")) { - const [t, args, opt] = parseArgs(tokens[i].slice(1), context, vars); + const [t, args, opt] = parseArgs(tokens[i].replace(/^>{1,2}/, ""), context, vars); + const blockContent = []; // to store partial block content + if (tokens[i].startsWith(">>")) { + i = compile(tokens, blockContent, context, vars, i + 1, t); + } if (typeof partials[t] === "string") { const newCtx = args.length > 0 ? args[0] : (Object.keys(opt).length > 0 ? opt : context); - compile(tokenize(partials[t]), output, newCtx, vars, 0, ""); + const newVars = {...vars, content: blockContent.join("")}; + compile(tokenize(partials[t]), output, newCtx, newVars, 0, ""); } } else if (tokens[i].startsWith("=")) { From a486f129059b144b706b46c946c2b47670a7d90d Mon Sep 17 00:00:00 2001 From: Josemi Juanes <5751201+jmjuanes@users.noreply.github.com> Date: Tue, 24 Dec 2024 17:12:22 +0100 Subject: [PATCH 2/3] Add test for block partials --- test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test.js b/test.js index b9c5fdf..0be367f 100644 --- a/test.js +++ b/test.js @@ -164,6 +164,16 @@ describe("templating", () => { }); }); + describe("{{>> xyz}}", () => { + it("should allow providing a block to the partial", () => { + const partials = { + foo: "Hello {{@content}}!", + }; + + assert.equal(m("{{>>foo}}Bob{{/foo}}", {}, {partials}), "Hello Bob!"); + }); + }); + describe("{{#each }}", () => { it("should do nothing if value is not an array or object", () => { assert.equal(m("x{{#each values}}{{.}}{{/each}}x", {values: null}), "xx"); From c6562613041dc84094977be148739dff3686aaeb Mon Sep 17 00:00:00 2001 From: Josemi Juanes <5751201+jmjuanes@users.noreply.github.com> Date: Tue, 24 Dec 2024 17:54:13 +0100 Subject: [PATCH 3/3] Add documentation for partial blocks --- README.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 70fb30f..eecfb6a 100644 --- a/README.md +++ b/README.md @@ -142,9 +142,28 @@ const result = m("User: {{>user userName=name userEmail=email }}", data, {partia Please note that providing keyword arguments and a custom context to a partial is not supported. On this situation, the partial will be evaluated only with the custom context. +#### Partial blocks + +> This feature was added in `v0.16.0`. + +You can pass a block to a partial using a greather than symbol `>>` followed by the partial name to start the partial block, and a slash followed by the partial name to end the partial block. The provided block content will be available in the `@content` variable. + +Example: + +```javascript +const options = { + partials: { + foo: "Hello {{@content}}!", + }, +}; + +const result = m("{{>>foo}}Bob{{/foo}}", {}, options); +// Output: 'Hello Bob!' +``` + ### Inline partials -> Added in `v0.16.0`. +> This feature was added in `v0.16.0`. Inline partials allows you to define partials directly in your template. Use the plus symbol `+` followed by the partial name to start the partial definition, and end the partial definition with a slash `/` followed by the partial name. For example, `{{