Skip to content

Commit

Permalink
Add support for layout parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed Sep 21, 2020
1 parent 7aea7e0 commit ccac9f7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 40 deletions.
8 changes: 4 additions & 4 deletions deno_dist/compile-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ export default function compileToString(
): string {
var buffer: Array<AstObject> = Parse(str, config);

var res = "var tR='',__l" +
var res = "var tR='',__l,__lP" +
(config.include ? ",include=E.include.bind(E)" : "") +
(config.includeFile ? ",includeFile=E.includeFile.bind(E)" : "") +
"\nfunction layout(p){__l=p}\n" +
"\nfunction layout(p,d){__l=p;__lP=d}\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`
`includeFile(__l,Object.assign(__lP||${config.varName},{body:tR}))\n`
: config.include
? "if(__l)tR=" +
(config.async ? "await " : "") +
`include(__l,Object.assign(${config.varName},{body:tR}))\n`
`include(__l,Object.assign(__lP||${config.varName},{body:tR}))\n`
: "") +
"if(cb){cb(null,tR)} return tR" +
(config.useWith ? "}" : "");
Expand Down
8 changes: 4 additions & 4 deletions src/compile-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ export default function compileToString(str: string, config: EtaConfig): string
var buffer: Array<AstObject> = Parse(str, config)

var res =
"var tR='',__l" +
"var tR='',__l,__lP" +
(config.include ? ',include=E.include.bind(E)' : '') +
(config.includeFile ? ',includeFile=E.includeFile.bind(E)' : '') +
'\nfunction layout(p){__l=p}\n' +
'\nfunction layout(p,d){__l=p;__lP=d}\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`
`includeFile(__l,Object.assign(__lP||${config.varName},{body:tR}))\n`
: config.include
? 'if(__l)tR=' +
(config.async ? 'await ' : '') +
`include(__l,Object.assign(${config.varName},{body:tR}))\n`
`include(__l,Object.assign(__lP||${config.varName},{body:tR}))\n`
: '') +
'if(cb){cb(null,tR)} return tR' +
(config.useWith ? '}' : '')
Expand Down
12 changes: 6 additions & 6 deletions test/async.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ describe('Async Render checks', () => {
await Eta.render('<%= @#$%^ %>', {}, { async: true })
}).rejects.toThrow(
buildRegEx(`
var tR='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){__l=p}
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(it,{body:tR}))
if(__l)tR=await includeFile(__l,Object.assign(__lP||it,{body:tR}))
if(cb){cb(null,tR)} return tR
`)
)
Expand All @@ -61,10 +61,10 @@ if(cb){cb(null,tR)} return tR
expect(err).toBeTruthy()
expect((err as Error).message).toMatch(
buildRegEx(`
var tR='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){__l=p}
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(it,{body:tR}))
if(__l)tR=await includeFile(__l,Object.assign(__lP||it,{body:tR}))
if(cb){cb(null,tR)} return tR
`)
)
Expand Down
28 changes: 14 additions & 14 deletions test/compile-string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ 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='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){__l=p}
expect(str).toEqual(`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(hey)
if(__l)tR=includeFile(__l,Object.assign(it,{body:tR}))
if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR}))
if(cb){cb(null,tR)} return tR`)
})

Expand All @@ -23,39 +23,39 @@ if(cb){cb(null,tR)} return tR`)
'hi <%= hey %>',
getConfig({ include: undefined, includeFile: undefined })
)
expect(str).toEqual(`var tR='',__l
function layout(p){__l=p}
expect(str).toEqual(`var tR='',__l,__lP
function layout(p,d){__l=p;__lP=d}
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='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){__l=p}
expect(str).toEqual(`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+=hey
if(__l)tR=includeFile(__l,Object.assign(it,{body:tR}))
if(__l)tR=includeFile(__l,Object.assign(__lP||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='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p){__l=p}
expect(str).toEqual(`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(hey)
tR+=E.e(hi)
if(__l)tR=includeFile(__l,Object.assign(it,{body:tR}))
if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR}))
if(cb){cb(null,tR)} return tR`)
})

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

Expand Down

0 comments on commit ccac9f7

Please sign in to comment.