Skip to content

Commit

Permalink
Change order of data merge for layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed Sep 21, 2020
1 parent 2b27782 commit f14b609
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
4 changes: 2 additions & 2 deletions deno_dist/compile-string.ts
Expand Up @@ -33,11 +33,11 @@ export default function compileToString(
(config.includeFile
? "if(__l)tR=" +
(config.async ? "await " : "") +
`includeFile(__l,Object.assign(__lP||${config.varName},{body:tR}))\n`
`includeFile(__l,Object.assign(${config.varName},{body:tR},__lP))\n`
: config.include
? "if(__l)tR=" +
(config.async ? "await " : "") +
`include(__l,Object.assign(__lP||${config.varName},{body:tR}))\n`
`include(__l,Object.assign(${config.varName},{body:tR},__lP))\n`
: "") +
"if(cb){cb(null,tR)} return tR" +
(config.useWith ? "}" : "");
Expand Down
4 changes: 2 additions & 2 deletions src/compile-string.ts
Expand Up @@ -31,11 +31,11 @@ export default function compileToString(str: string, config: EtaConfig): string
(config.includeFile
? 'if(__l)tR=' +
(config.async ? 'await ' : '') +
`includeFile(__l,Object.assign(__lP||${config.varName},{body:tR}))\n`
`includeFile(__l,Object.assign(${config.varName},{body:tR},__lP))\n`
: config.include
? 'if(__l)tR=' +
(config.async ? 'await ' : '') +
`include(__l,Object.assign(__lP||${config.varName},{body:tR}))\n`
`include(__l,Object.assign(${config.varName},{body:tR},__lP))\n`
: '') +
'if(cb){cb(null,tR)} return tR' +
(config.useWith ? '}' : '')
Expand Down
4 changes: 2 additions & 2 deletions test/async.spec.ts
Expand Up @@ -50,7 +50,7 @@ describe('Async Render checks', () => {
var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p,d){__l=p;__lP=d}
tR+=E.e(@#$%^)
if(__l)tR=await includeFile(__l,Object.assign(__lP||it,{body:tR}))
if(__l)tR=await includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR
`)
)
Expand All @@ -64,7 +64,7 @@ if(cb){cb(null,tR)} return tR
var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p,d){__l=p;__lP=d}
tR+=E.e(@#$%^)
if(__l)tR=await includeFile(__l,Object.assign(__lP||it,{body:tR}))
if(__l)tR=await includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR
`)
)
Expand Down
8 changes: 4 additions & 4 deletions test/compile-string.spec.ts
Expand Up @@ -15,7 +15,7 @@ describe('Compile to String test', () => {
function layout(p,d){__l=p;__lP=d}
tR+='hi '
tR+=E.e(hey)
if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR}))
if(__l)tR=includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR`)
})

Expand All @@ -38,7 +38,7 @@ if(cb){cb(null,tR)} return tR`)
function layout(p,d){__l=p;__lP=d}
tR+='hi '
tR+=hey
if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR}))
if(__l)tR=includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR`)
})

Expand All @@ -50,7 +50,7 @@ function layout(p,d){__l=p;__lP=d}
tR+='hi'
tR+=E.e(hey)
tR+=E.e(hi)
if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR}))
if(__l)tR=includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR`)
})

Expand Down Expand Up @@ -82,7 +82,7 @@ tR+=' \\n '
}
tR+='\\nThis is a partial: '
tR+=include("mypartial")
if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR}))
if(__l)tR=includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR`
)
})
Expand Down
2 changes: 1 addition & 1 deletion test/compile.spec.ts
Expand Up @@ -34,7 +34,7 @@ describe('Compile test', () => {
var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p,d){__l=p;__lP=d}
hi (=h)
if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR}))
if(__l)tR=includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR
`)
)
Expand Down
6 changes: 3 additions & 3 deletions test/file-handlers.spec.ts
Expand Up @@ -111,7 +111,7 @@ var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p,d){__l=p;__lP=d}
tR+='Hi '
tR+=E.e(badSyntax(=!)
if(__l)tR=await includeFile(__l,Object.assign(__lP||it,{body:tR}))
if(__l)tR=await includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR
`)
)
Expand All @@ -130,7 +130,7 @@ var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p,d){__l=p;__lP=d}
tR+='Hi '
tR+=E.e(badSyntax(=!)
if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR}))
if(__l)tR=includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR
`)
)
Expand All @@ -150,7 +150,7 @@ var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p,d){__l=p;__lP=d}
tR+='Hi '
tR+=E.e(badSyntax(=!)
if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR}))
if(__l)tR=includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR
*/

Expand Down
25 changes: 23 additions & 2 deletions test/layouts.spec.ts
Expand Up @@ -7,15 +7,15 @@ describe('Layout Tests', () => {
it('Nested layouts work as expected', async () => {
var res = await renderFile(
'index.eta',
{ title: 'Cool Title' },
{ title: 'Cool Title' },
// Async can be true or false
{ views: path.join(__dirname, 'templates'), async: true }
)

expect(res).toEqual(`<!DOCTYPE html>
<html lang="en">
<head>
<title>Cool Title</title>
<title>Cool Title</title>
</head>
<body>
This is the template body.
Expand All @@ -38,4 +38,25 @@ This is a layout`,

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
})
)

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

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

expect(res).toEqual('Nifty title - This is a layout - Nice content - 3')
})
})

0 comments on commit f14b609

Please sign in to comment.