Skip to content

Commit

Permalink
Add support for layouts ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed Sep 17, 2020
1 parent b42a56d commit 5c2fda2
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 15 deletions.
13 changes: 11 additions & 2 deletions deno_dist/compile-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@ export default function compileToString(
): string {
var buffer: Array<AstObject> = Parse(str, config);

var res = "var tR=''" +
var res = "var tR='',l" +
(config.include ? ",include=E.include.bind(E)" : "") +
(config.includeFile ? ",includeFile=E.includeFile.bind(E)" : "") +
"\n" +
"\nfunction layout(p){l=p}\n" +
(config.useWith ? "with(" + config.varName + "||{}){" : "") +
compileScope(buffer, config) +
(config.includeFile
? "if(l)tR=" +
(config.async ? "await " : "") +
`includeFile(l,Object.assign(${config.varName},{body:tR}))\n`
: config.include
? "if(l)tR=" +
(config.async ? "await " : "") +
`include(l,Object.assign(${config.varName},{body:tR}))\n`
: "") +
"if(cb){cb(null,tR)} return tR" +
(config.useWith ? "}" : "");

Expand Down
13 changes: 11 additions & 2 deletions src/compile-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,21 @@ export default function compileToString(str: string, config: EtaConfig): string
var buffer: Array<AstObject> = Parse(str, config)

var res =
"var tR=''" +
"var tR='',l" +
(config.include ? ',include=E.include.bind(E)' : '') +
(config.includeFile ? ',includeFile=E.includeFile.bind(E)' : '') +
'\n' +
'\nfunction layout(p){l=p}\n' +
(config.useWith ? 'with(' + config.varName + '||{}){' : '') +
compileScope(buffer, config) +
(config.includeFile
? 'if(l)tR=' +
(config.async ? 'await ' : '') +
`includeFile(l,Object.assign(${config.varName},{body:tR}))\n`
: config.include
? 'if(l)tR=' +
(config.async ? 'await ' : '') +
`include(l,Object.assign(${config.varName},{body:tR}))\n`
: '') +
'if(cb){cb(null,tR)} return tR' +
(config.useWith ? '}' : '')

Expand Down
8 changes: 6 additions & 2 deletions test/async.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ describe('Async Render checks', () => {
await Eta.render('<%= @#$%^ %>', {}, { async: true })
}).rejects.toThrow(
buildRegEx(`
var tR='',include=E.include.bind(E),includeFile=E.includeFile.bind(E)
var tR='',l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){l=p}
tR+=E.e(@#$%^)
if(l)tR=await includeFile(l,Object.assign(it,{body:tR}))
if(cb){cb(null,tR)} return tR
`)
)
Expand All @@ -59,8 +61,10 @@ if(cb){cb(null,tR)} return tR
expect(err).toBeTruthy()
expect((err as Error).message).toMatch(
buildRegEx(`
var tR='',include=E.include.bind(E),includeFile=E.includeFile.bind(E)
var tR='',l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){l=p}
tR+=E.e(@#$%^)
if(l)tR=await includeFile(l,Object.assign(it,{body:tR}))
if(cb){cb(null,tR)} return tR
`)
)
Expand Down
22 changes: 17 additions & 5 deletions test/compile-string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ const complexTemplate = fs.readFileSync(filePath, 'utf8')
describe('Compile to String test', () => {
it('parses a simple template', () => {
var str = compileToString('hi <%= hey %>', defaultConfig)
expect(str).toEqual(`var tR='',include=E.include.bind(E),includeFile=E.includeFile.bind(E)
expect(str)
.toEqual(`var tR='',l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){l=p}
tR+='hi '
tR+=E.e(hey)
if(l)tR=includeFile(l,Object.assign(it,{body:tR}))
if(cb){cb(null,tR)} return tR`)
})

Expand All @@ -21,33 +24,41 @@ if(cb){cb(null,tR)} return tR`)
'hi <%= hey %>',
getConfig({ include: undefined, includeFile: undefined })
)
expect(str).toEqual(`var tR=''
expect(str).toEqual(`var tR='',l
function layout(p){l=p}
tR+='hi '
tR+=E.e(hey)
if(cb){cb(null,tR)} return tR`)
})

it('parses a simple template with raw tag', () => {
var str = compileToString('hi <%~ hey %>', defaultConfig)
expect(str).toEqual(`var tR='',include=E.include.bind(E),includeFile=E.includeFile.bind(E)
expect(str)
.toEqual(`var tR='',l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){l=p}
tR+='hi '
tR+=hey
if(l)tR=includeFile(l,Object.assign(it,{body:tR}))
if(cb){cb(null,tR)} return tR`)
})

it('works with whitespace trimming', () => {
var str = compileToString('hi\n<%- = hey-%>\n<%_ = hi_%>', defaultConfig)
expect(str).toEqual(`var tR='',include=E.include.bind(E),includeFile=E.includeFile.bind(E)
expect(str)
.toEqual(`var tR='',l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){l=p}
tR+='hi'
tR+=E.e(hey)
tR+=E.e(hi)
if(l)tR=includeFile(l,Object.assign(it,{body:tR}))
if(cb){cb(null,tR)} return tR`)
})

it('compiles complex template', () => {
var str = compileToString(complexTemplate, defaultConfig)
expect(str).toEqual(
`var tR='',include=E.include.bind(E),includeFile=E.includeFile.bind(E)
`var tR='',l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){l=p}
tR+='Hi\\n'
console.log("Hope you like Eta!")
tR+=E.e(it.htmlstuff)
Expand All @@ -71,6 +82,7 @@ tR+=' \\n '
}
tR+='\\nThis is a partial: '
tR+=include("mypartial")
if(l)tR=includeFile(l,Object.assign(it,{body:tR}))
if(cb){cb(null,tR)} return tR`
)
})
Expand Down
4 changes: 3 additions & 1 deletion test/compile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ describe('Compile test', () => {
compile('<% hi (=h) %>')
}).toThrow(
buildRegEx(`
var tR='',include=E.include.bind(E),includeFile=E.includeFile.bind(E)
var tR='',l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){l=p}
hi (=h)
if(l)tR=includeFile(l,Object.assign(it,{body:tR}))
if(cb){cb(null,tR)} return tR
`)
)
Expand Down
12 changes: 9 additions & 3 deletions test/file-handlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ describe('renderFile error tests', () => {
expect(err).toBeTruthy()
expect(err.message).toMatch(
buildRegEx(`
var tR='',include=E.include.bind(E),includeFile=E.includeFile.bind(E)
var tR='',l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){l=p}
tR+='Hi '
tR+=E.e(badSyntax(=!)
if(l)tR=await includeFile(l,Object.assign(it,{body:tR}))
if(cb){cb(null,tR)} return tR
`)
)
Expand All @@ -124,9 +126,11 @@ if(cb){cb(null,tR)} return tR
await renderFile(errFilePath, {})
}).rejects.toThrow(
buildRegEx(`
var tR='',include=E.include.bind(E),includeFile=E.includeFile.bind(E)
var tR='',l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){l=p}
tR+='Hi '
tR+=E.e(badSyntax(=!)
if(l)tR=includeFile(l,Object.assign(it,{body:tR}))
if(cb){cb(null,tR)} return tR
`)
)
Expand All @@ -142,9 +146,11 @@ Bad template syntax
Unexpected token '='
====================
var tR='',include=E.include.bind(E),includeFile=E.includeFile.bind(E)
var tR='',l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){l=p}
tR+='Hi '
tR+=E.e(badSyntax(=!)
if(l)tR=includeFile(l,Object.assign(it,{body:tR}))
if(cb){cb(null,tR)} return tR
*/

Expand Down
25 changes: 25 additions & 0 deletions test/layouts.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { path } from '../src/file-methods'
/* global it, expect, describe */

import { renderFile } from '../src/index'

describe('Layout Tests', () => {
it('Layout works as expected', async () => {
var res = await renderFile(
'index.eta',
{ title: 'Cool Title' },
// Async can be true or false
{ views: path.join(__dirname, 'templates'), async: false }
)

expect(res).toEqual(`<!DOCTYPE html>
<html lang="en">
<head>
<title>Cool Title</title>
</head>
<body>
This is the template body.
</body>
</html>`)
})
})
2 changes: 2 additions & 0 deletions test/templates/index.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<% layout('./layout') %>
This is the template body.
7 changes: 7 additions & 0 deletions test/templates/layout.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% layout('./outer-layout') %>
<head>
<title><%= it.title %></title>
</head>
<body>
<%~ it.body %>
</body>
4 changes: 4 additions & 0 deletions test/templates/outer-layout.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!DOCTYPE html>
<html lang="en">
<%~ it.body %>
</html>

0 comments on commit 5c2fda2

Please sign in to comment.