mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-17 21:40:39 -07:00
feat: renderSync, parseAndRenderSync and renderFileSync, see #48
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
@@ -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
|
||||
|
||||
+18
-11
@@ -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) {
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
+18
-3
@@ -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 = new Expression(branch.cond).value(ctx)
|
||||
const cond = await new Expression(branch.cond).value(ctx)
|
||||
if (isTruthy(cond)) {
|
||||
await this.liquid.renderer.renderTemplates(branch.templates, ctx, emitter)
|
||||
await r.renderTemplates(branch.templates, ctx, emitter)
|
||||
return
|
||||
}
|
||||
}
|
||||
await 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 (const branch of this.branches) {
|
||||
const cond = new Expression(branch.cond).valueSync(ctx)
|
||||
if (isTruthy(cond)) {
|
||||
r.renderTemplatesSync(branch.templates, ctx, emitter)
|
||||
return
|
||||
}
|
||||
}
|
||||
r.renderTemplatesSync(this.elseTemplates, ctx, emitter)
|
||||
}
|
||||
} as ITagImplOptions
|
||||
|
||||
@@ -17,14 +17,14 @@ export default {
|
||||
match = withRE.exec(token.args)
|
||||
if (match) this.with = match[1]
|
||||
},
|
||||
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
|
||||
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 = await this.liquid.parseAndRender(template, ctx.getAll(), ctx.opts)
|
||||
filepath = this.liquid.parseAndRenderSync(template, ctx.getAll(), ctx.opts)
|
||||
} else {
|
||||
filepath = new Expression(this.value).value(ctx)
|
||||
filepath = new Expression(this.value).valueSync(ctx)
|
||||
}
|
||||
} else {
|
||||
filepath = this.staticValue
|
||||
@@ -37,7 +37,37 @@ export default {
|
||||
ctx.setRegister('blocks', {})
|
||||
ctx.setRegister('blockMode', BlockMode.OUTPUT)
|
||||
if (this.with) {
|
||||
hash[filepath] = new Expression(this.with).evaluate(ctx)
|
||||
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 = await new Expression(this.value).value(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] = await new Expression(this.with).evaluate(ctx)
|
||||
}
|
||||
const templates = await this.liquid.getTemplate(filepath, ctx.opts)
|
||||
ctx.push(hash)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
@@ -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>')
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user