Skip to content

Commit

Permalink
feat: renderSync, parseAndRenderSync and renderFileSync, see #48
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Aug 26, 2019
1 parent 8028f82 commit 7fb01ad
Show file tree
Hide file tree
Showing 66 changed files with 891 additions and 267 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"prefer-const": 2,
"no-unused-vars": "off",
"indent": "off",
"no-dupe-class-members": "off",
"@typescript-eslint/indent": ["error", 2],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any": "off",
Expand Down
6 changes: 3 additions & 3 deletions demo/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ engine.registerTag('header', {
const [key, val] = token.args.split(':')
this[key] = val
},
render: function (scope, hash) {
const title = this.liquid.evalValue(this.content, scope)
return `<h1>${title}</h1>`
render: async function (scope, hash, emitter) {
const title = await this.liquid.evalValue(this.content, scope)
emitter.write(`<h1>${title}</h1>`)
}
})

Expand Down
8 changes: 4 additions & 4 deletions demo/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Liquid, TagToken, Hash, Context } from 'liquidjs'
import { Liquid, TagToken, Hash, Context, Emitter } from 'liquidjs'

const engine = new Liquid({
root: __dirname,
Expand All @@ -10,9 +10,9 @@ engine.registerTag('header', {
const [key, val] = token.args.split(':')
this[key] = val
},
render: function (context: Context, hash: Hash) {
const title = this.liquid.evalValue(this['content'], context)
return `<h1>${title}</h1>`
render: async function (context: Context, hash: Hash, emitter: Emitter) {
const title = await this.liquid.evalValue(this['content'], context)
emitter.write(`<h1>${title}</h1>`)
}
})

Expand Down
4 changes: 3 additions & 1 deletion src/builtin/tags/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default {
this.value = match[2]
},
render: async function (ctx: Context) {
ctx.front()[this.key] = await this.liquid.evalValue(this.value, ctx)
ctx.front()[this.key] = ctx.sync
? this.liquid.evalValueSync(this.value, ctx)
: await this.liquid.evalValue(this.value, ctx)
}
} as ITagImplOptions
6 changes: 5 additions & 1 deletion src/builtin/tags/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ export default {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const blocks = ctx.getRegister('blocks')
const childDefined = blocks[this.block]
const r = this.liquid.renderer
const html = childDefined !== undefined
? childDefined
: await this.liquid.renderer.renderTemplates(this.tpls, ctx)
: (ctx.sync
? r.renderTemplatesSync(this.tpls, ctx)
: await r.renderTemplates(this.tpls, ctx)
)

if (ctx.getRegister('blockMode', BlockMode.OUTPUT) === BlockMode.STORE) {
blocks[this.block] = html
Expand Down
6 changes: 3 additions & 3 deletions src/builtin/tags/break.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RenderBreakError } from '../../util/error'
import { Emitter, Context, Hash } from '../../types'

export default {
render: async function () {
throw new RenderBreakError('break')
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
emitter.break = true
}
}
5 changes: 4 additions & 1 deletion src/builtin/tags/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export default {
stream.start()
},
render: async function (ctx: Context) {
const html = await this.liquid.renderer.renderTemplates(this.templates, ctx)
const r = this.liquid.renderer
const html = ctx.sync
? r.renderTemplatesSync(this.templates, ctx)
: await r.renderTemplates(this.templates, ctx)
ctx.front()[this.variable] = html
}
} as ITagImplOptions
23 changes: 19 additions & 4 deletions src/builtin/tags/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,30 @@ export default {
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
for (let i = 0; i < this.cases.length; i++) {
const branch = this.cases[i]
const val = new Expression(branch.val).value(ctx)
const cond = new Expression(this.cond).value(ctx)
const val = await new Expression(branch.val).value(ctx)
const cond = await new Expression(this.cond).value(ctx)
if (val === cond) {
this.liquid.renderer.renderTemplates(branch.templates, ctx, emitter)
await r.renderTemplates(branch.templates, ctx, emitter)
return
}
}
this.liquid.renderer.renderTemplates(this.elseTemplates, ctx, emitter)
await r.renderTemplates(this.elseTemplates, ctx, emitter)
},

renderSync: function (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
for (let i = 0; i < this.cases.length; i++) {
const branch = this.cases[i]
const val = new Expression(branch.val).valueSync(ctx)
const cond = new Expression(this.cond).valueSync(ctx)
if (val === cond) {
r.renderTemplatesSync(branch.templates, ctx, emitter)
return
}
}
r.renderTemplatesSync(this.elseTemplates, ctx, emitter)
}
} as ITagImplOptions
6 changes: 3 additions & 3 deletions src/builtin/tags/continue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RenderBreakError } from '../../util/error'
import { Emitter, Context, Hash } from '../../types'

export default {
render: async function () {
throw new RenderBreakError('continue')
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
emitter.continue = true
}
}
9 changes: 7 additions & 2 deletions src/builtin/tags/cycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export default {
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const group = this.group.value(ctx)
const group = ctx.sync
? this.group.valueSync(ctx)
: await this.group.value(ctx)
const fingerprint = `cycle:${group}:` + this.candidates.join(',')
const groups = ctx.getRegister('cycle')
let idx = groups[fingerprint]
Expand All @@ -34,6 +36,9 @@ export default {
const candidate = this.candidates[idx]
idx = (idx + 1) % this.candidates.length
groups[fingerprint] = idx
emitter.write(new Expression(candidate).value(ctx))
const html = ctx.sync
? new Expression(candidate).valueSync(ctx)
: await new Expression(candidate).value(ctx)
emitter.write(html)
}
} as ITagImplOptions
29 changes: 18 additions & 11 deletions src/builtin/tags/for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export default {
stream.start()
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let collection = new Expression(this.collection).value(ctx)
const r = this.liquid.renderer
let collection = ctx.sync
? new Expression(this.collection).valueSync(ctx)
: await new Expression(this.collection).value(ctx)

if (!isArray(collection)) {
if (isString(collection) && collection.length > 0) {
Expand All @@ -47,7 +50,9 @@ export default {
}
}
if (!isArray(collection) || !collection.length) {
this.liquid.renderer.renderTemplates(this.elseTemplates, ctx, emitter)
ctx.sync
? r.renderTemplatesSync(this.elseTemplates, ctx, emitter)
: await r.renderTemplates(this.elseTemplates, ctx, emitter)
return
}

Expand All @@ -57,17 +62,19 @@ export default {
collection = collection.slice(offset, offset + limit)
if (this.reversed) collection.reverse()

const context = { forloop: new ForloopDrop(collection.length) }
ctx.push(context)
const scope = { forloop: new ForloopDrop(collection.length) }
ctx.push(scope)
for (const item of collection) {
context[this.variable] = item
try {
await this.liquid.renderer.renderTemplates(this.templates, ctx, emitter)
} catch (e) {
if (e.name !== 'RenderBreakError') throw e
if (e.message === 'break') break
scope[this.variable] = item
ctx.sync
? r.renderTemplatesSync(this.templates, ctx, emitter)
: await r.renderTemplates(this.templates, ctx, emitter)
if (emitter.break) {
emitter.break = false
break
}
context.forloop.next()
emitter.continue = false
scope.forloop.next()
}
ctx.pop()
}
Expand Down
21 changes: 18 additions & 3 deletions src/builtin/tags/if.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,28 @@ export default {
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer

for (const branch of this.branches) {
const cond = await new Expression(branch.cond).value(ctx)
if (isTruthy(cond)) {
await r.renderTemplates(branch.templates, ctx, emitter)
return
}
}
await r.renderTemplates(this.elseTemplates, ctx, emitter)
},

renderSync: function (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer

for (const branch of this.branches) {
const cond = new Expression(branch.cond).value(ctx)
const cond = new Expression(branch.cond).valueSync(ctx)
if (isTruthy(cond)) {
await this.liquid.renderer.renderTemplates(branch.templates, ctx, emitter)
r.renderTemplatesSync(branch.templates, ctx, emitter)
return
}
}
await this.liquid.renderer.renderTemplates(this.elseTemplates, ctx, emitter)
r.renderTemplatesSync(this.elseTemplates, ctx, emitter)
}
} as ITagImplOptions
34 changes: 32 additions & 2 deletions src/builtin/tags/include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,44 @@ export default {
match = withRE.exec(token.args)
if (match) this.with = match[1]
},
renderSync: function (ctx: Context, hash: Hash, emitter: Emitter) {
let filepath
if (ctx.opts.dynamicPartials) {
if (quotedLine.exec(this.value)) {
const template = this.value.slice(1, -1)
filepath = this.liquid.parseAndRenderSync(template, ctx.getAll(), ctx.opts)
} else {
filepath = new Expression(this.value).valueSync(ctx)
}
} else {
filepath = this.staticValue
}
assert(filepath, `cannot include with empty filename`)

const originBlocks = ctx.getRegister('blocks')
const originBlockMode = ctx.getRegister('blockMode')

ctx.setRegister('blocks', {})
ctx.setRegister('blockMode', BlockMode.OUTPUT)
if (this.with) {
hash[filepath] = new Expression(this.with).evaluateSync(ctx)
}
const templates = this.liquid.getTemplateSync(filepath, ctx.opts)
ctx.push(hash)
this.liquid.renderer.renderTemplatesSync(templates, ctx, emitter)
ctx.pop()
ctx.setRegister('blocks', originBlocks)
ctx.setRegister('blockMode', originBlockMode)
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let filepath
if (ctx.opts.dynamicPartials) {
if (quotedLine.exec(this.value)) {
const template = this.value.slice(1, -1)
filepath = await this.liquid.parseAndRender(template, ctx.getAll(), ctx.opts)
} else {
filepath = new Expression(this.value).value(ctx)
filepath = await new Expression(this.value).value(ctx)
}
} else {
filepath = this.staticValue
Expand All @@ -37,7 +67,7 @@ export default {
ctx.setRegister('blocks', {})
ctx.setRegister('blockMode', BlockMode.OUTPUT)
if (this.with) {
hash[filepath] = new Expression(this.with).evaluate(ctx)
hash[filepath] = await new Expression(this.with).evaluate(ctx)
}
const templates = await this.liquid.getTemplate(filepath, ctx.opts)
ctx.push(hash)
Expand Down
18 changes: 14 additions & 4 deletions src/builtin/tags/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,31 @@ export default {
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const layout = ctx.opts.dynamicPartials
? await (new Expression(this.layout).value(ctx))
? (ctx.sync
? new Expression(this.layout).valueSync(ctx)
: await new Expression(this.layout).value(ctx)
)
: this.staticLayout
assert(layout, `cannot apply layout with empty filename`)

// render the remaining tokens immediately
ctx.setRegister('blockMode', BlockMode.STORE)
const blocks = ctx.getRegister('blocks')
const html = await this.liquid.renderer.renderTemplates(this.tpls, ctx)
const r = this.liquid.renderer
const html = ctx.sync
? r.renderTemplatesSync(this.tpls, ctx)
: await r.renderTemplates(this.tpls, ctx)
if (blocks[''] === undefined) {
blocks[''] = html
}
const templates = await this.liquid.getTemplate(layout, ctx.opts)
const templates = ctx.sync
? this.liquid.getTemplateSync(layout, ctx.opts)
: await this.liquid.getTemplate(layout, ctx.opts)
ctx.push(hash)
ctx.setRegister('blockMode', BlockMode.OUTPUT)
const partial = await this.liquid.renderer.renderTemplates(templates, ctx)
const partial = ctx.sync
? r.renderTemplatesSync(templates, ctx)
: await r.renderTemplates(templates, ctx)
ctx.pop()
emitter.write(partial)
}
Expand Down
4 changes: 2 additions & 2 deletions src/builtin/tags/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {
})
stream.start()
},
render: function (ctx: Context, hash: Hash, emitter: Emitter) {
emitter.write(this.tokens.map((token: Token) => token.raw).join(''))
render: function (ctx: Context) {
return this.tokens.map((token: Token) => token.raw).join('')
}
} as ITagImplOptions
9 changes: 7 additions & 2 deletions src/builtin/tags/tablerow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ export default {
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let collection = new Expression(this.collection).value(ctx) || []
let collection = ctx.sync
? new Expression(this.collection).valueSync(ctx) || []
: await new Expression(this.collection).value(ctx) || []
const offset = hash.offset || 0
const limit = (hash.limit === undefined) ? collection.length : hash.limit

collection = collection.slice(offset, offset + limit)
const cols = hash.cols || collection.length

const r = this.liquid.renderer
const tablerowloop = new TablerowloopDrop(collection.length, cols)
const scope = { tablerowloop }
ctx.push(scope)
Expand All @@ -47,7 +50,9 @@ export default {
emitter.write(`<tr class="row${tablerowloop.row()}">`)
}
emitter.write(`<td class="col${tablerowloop.col()}">`)
await this.liquid.renderer.renderTemplates(this.templates, ctx, emitter)
ctx.sync
? r.renderTemplatesSync(this.templates, ctx, emitter)
: await r.renderTemplates(this.templates, ctx, emitter)
emitter.write('</td>')
}
if (collection.length) emitter.write('</tr>')
Expand Down
17 changes: 13 additions & 4 deletions src/builtin/tags/unless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@ export default {
stream.start()
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const cond = new Expression(this.cond).value(ctx)
renderSync: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
const cond = new Expression(this.cond).valueSync(ctx)
isFalsy(cond)
? await this.liquid.renderer.renderTemplates(this.templates, ctx, emitter)
: await this.liquid.renderer.renderTemplates(this.elseTemplates, ctx, emitter)
? r.renderTemplatesSync(this.templates, ctx, emitter)
: r.renderTemplatesSync(this.elseTemplates, ctx, emitter)
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
const cond = await new Expression(this.cond).value(ctx)
await isFalsy(cond)
? r.renderTemplates(this.templates, ctx, emitter)
: r.renderTemplates(this.elseTemplates, ctx, emitter)
}
} as ITagImplOptions
Loading

0 comments on commit 7fb01ad

Please sign in to comment.