Skip to content

Commit

Permalink
fix: merge body:__eta.res after ...it
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed May 29, 2023
1 parent e715f07 commit 83d56ef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/compile-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ ${config.useWith ? "with(" + config.varName + "||{}){" : ""}
${compileBody.call(this, buffer)}
if (__eta.layout) {
__eta.res = ${isAsync ? "await includeAsync" : "include"} (__eta.layout, {body: __eta.res, ...${
__eta.res = ${isAsync ? "await includeAsync" : "include"} (__eta.layout, {...${
config.varName
}, ...__eta.layoutData});
}, body: __eta.res, ...__eta.layoutData});
}
${config.useWith ? "}" : ""}
Expand Down
8 changes: 4 additions & 4 deletions test/compile-string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ __eta.res+=__eta.e(it.name)
if (__eta.layout) {
__eta.res = include (__eta.layout, {body: __eta.res, ...it, ...__eta.layoutData});
__eta.res = include (__eta.layout, {...it, body: __eta.res, ...__eta.layoutData});
}
Expand Down Expand Up @@ -60,7 +60,7 @@ __eta.res+=it.name
if (__eta.layout) {
__eta.res = include (__eta.layout, {body: __eta.res, ...it, ...__eta.layoutData});
__eta.res = include (__eta.layout, {...it, body: __eta.res, ...__eta.layoutData});
}
Expand Down Expand Up @@ -90,7 +90,7 @@ __eta.res+=__eta.e(it.lastname)
if (__eta.layout) {
__eta.res = include (__eta.layout, {body: __eta.res, ...it, ...__eta.layoutData});
__eta.res = include (__eta.layout, {...it, body: __eta.res, ...__eta.layoutData});
}
Expand Down Expand Up @@ -140,7 +140,7 @@ __eta.res+=include("mypartial")
if (__eta.layout) {
__eta.res = include (__eta.layout, {body: __eta.res, ...it, ...__eta.layoutData});
__eta.res = include (__eta.layout, {...it, body: __eta.res, ...__eta.layoutData});
}
Expand Down
49 changes: 14 additions & 35 deletions test/layouts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { path } from "../src/file-methods";
/* global it, expect, describe */

import { compile, render, renderFile, templates } from "../src/index";
import path from "node:path";

import { Eta } from "../src/index";

const eta = new Eta({ views: path.join(__dirname, "templates") });

describe("Layout Tests", () => {
it("Nested layouts work as expected", async () => {
const res = await renderFile(
"index.eta",
{ title: "Cool Title" },
// Async can be true or false
{ views: path.join(__dirname, "templates"), async: true }
);
it("Nested layouts work as expected", () => {
const res = eta.render("index.eta", { title: "Cool Title" });

expect(res).toEqual(`<!DOCTYPE html>
<html lang="en">
Expand All @@ -23,39 +21,20 @@ This is the template body.
</html>`);
});

it("Layouts fall back to include if includeFile is undefined", async () => {
templates.define(
"my-layout",
compile(`###<%= it.title %>###,<%~ it.body %>`, { includeFile: undefined })
);

const res = await render(
`<% layout("my-layout") %>
This is a layout`,
{ title: "Cool Title" },
{ includeFile: undefined }
);

expect(res).toEqual("###Cool Title###,This is a layout");
});

it("Layouts are called with arguments if they're provided", async () => {
templates.define(
"my-layout",
compile(`<%= it.title %> - <%~ it.body %> - <%~ it.content %> - <%~ it.randomNum %>`, {
includeFile: undefined,
})
eta.loadTemplate(
"@my-layout",
`<%= it.title %> - <%~ it.body %> - <%~ it.content %> - <%~ it.randomNum %>`
);

const res = await render(
`<% layout("my-layout", { title: 'Nifty title', content: 'Nice content'}) %>
const res = await eta.renderString(
`<% layout("@my-layout", { title: 'Nifty title', content: 'Nice content'}) %>
This is a layout`,
{ title: "Cool Title", randomNum: 3 },
{ includeFile: undefined }
{ title: "Cool Title", randomNum: 3 }
);

// Note that layouts automatically accept the data of the template which called them,
// after it is merged with {body:tR} and custom data
// after it is merged with { body:__eta.res } and `it`

expect(res).toEqual("Nifty title - This is a layout - Nice content - 3");
});
Expand Down

0 comments on commit 83d56ef

Please sign in to comment.