Skip to content

Commit 7fb01ad

Browse files
committed
feat: renderSync, parseAndRenderSync and renderFileSync, see #48
1 parent 8028f82 commit 7fb01ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+891
-267
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"prefer-const": 2,
1919
"no-unused-vars": "off",
2020
"indent": "off",
21+
"no-dupe-class-members": "off",
2122
"@typescript-eslint/indent": ["error", 2],
2223
"@typescript-eslint/explicit-function-return-type": "off",
2324
"@typescript-eslint/no-explicit-any": "off",

demo/nodejs/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ engine.registerTag('header', {
1010
const [key, val] = token.args.split(':')
1111
this[key] = val
1212
},
13-
render: function (scope, hash) {
14-
const title = this.liquid.evalValue(this.content, scope)
15-
return `<h1>${title}</h1>`
13+
render: async function (scope, hash, emitter) {
14+
const title = await this.liquid.evalValue(this.content, scope)
15+
emitter.write(`<h1>${title}</h1>`)
1616
}
1717
})
1818

demo/typescript/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Liquid, TagToken, Hash, Context } from 'liquidjs'
1+
import { Liquid, TagToken, Hash, Context, Emitter } from 'liquidjs'
22

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

src/builtin/tags/assign.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export default {
1212
this.value = match[2]
1313
},
1414
render: async function (ctx: Context) {
15-
ctx.front()[this.key] = await this.liquid.evalValue(this.value, ctx)
15+
ctx.front()[this.key] = ctx.sync
16+
? this.liquid.evalValueSync(this.value, ctx)
17+
: await this.liquid.evalValue(this.value, ctx)
1618
}
1719
} as ITagImplOptions

src/builtin/tags/block.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ export default {
1717
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
1818
const blocks = ctx.getRegister('blocks')
1919
const childDefined = blocks[this.block]
20+
const r = this.liquid.renderer
2021
const html = childDefined !== undefined
2122
? childDefined
22-
: await this.liquid.renderer.renderTemplates(this.tpls, ctx)
23+
: (ctx.sync
24+
? r.renderTemplatesSync(this.tpls, ctx)
25+
: await r.renderTemplates(this.tpls, ctx)
26+
)
2327

2428
if (ctx.getRegister('blockMode', BlockMode.OUTPUT) === BlockMode.STORE) {
2529
blocks[this.block] = html

src/builtin/tags/break.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { RenderBreakError } from '../../util/error'
1+
import { Emitter, Context, Hash } from '../../types'
22

33
export default {
4-
render: async function () {
5-
throw new RenderBreakError('break')
4+
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
5+
emitter.break = true
66
}
77
}

src/builtin/tags/capture.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ export default {
2121
stream.start()
2222
},
2323
render: async function (ctx: Context) {
24-
const html = await this.liquid.renderer.renderTemplates(this.templates, ctx)
24+
const r = this.liquid.renderer
25+
const html = ctx.sync
26+
? r.renderTemplatesSync(this.templates, ctx)
27+
: await r.renderTemplates(this.templates, ctx)
2528
ctx.front()[this.variable] = html
2629
}
2730
} as ITagImplOptions

src/builtin/tags/case.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,30 @@ export default {
2525
},
2626

2727
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
28+
const r = this.liquid.renderer
2829
for (let i = 0; i < this.cases.length; i++) {
2930
const branch = this.cases[i]
30-
const val = new Expression(branch.val).value(ctx)
31-
const cond = new Expression(this.cond).value(ctx)
31+
const val = await new Expression(branch.val).value(ctx)
32+
const cond = await new Expression(this.cond).value(ctx)
3233
if (val === cond) {
33-
this.liquid.renderer.renderTemplates(branch.templates, ctx, emitter)
34+
await r.renderTemplates(branch.templates, ctx, emitter)
3435
return
3536
}
3637
}
37-
this.liquid.renderer.renderTemplates(this.elseTemplates, ctx, emitter)
38+
await r.renderTemplates(this.elseTemplates, ctx, emitter)
39+
},
40+
41+
renderSync: function (ctx: Context, hash: Hash, emitter: Emitter) {
42+
const r = this.liquid.renderer
43+
for (let i = 0; i < this.cases.length; i++) {
44+
const branch = this.cases[i]
45+
const val = new Expression(branch.val).valueSync(ctx)
46+
const cond = new Expression(this.cond).valueSync(ctx)
47+
if (val === cond) {
48+
r.renderTemplatesSync(branch.templates, ctx, emitter)
49+
return
50+
}
51+
}
52+
r.renderTemplatesSync(this.elseTemplates, ctx, emitter)
3853
}
3954
} as ITagImplOptions

src/builtin/tags/continue.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { RenderBreakError } from '../../util/error'
1+
import { Emitter, Context, Hash } from '../../types'
22

33
export default {
4-
render: async function () {
5-
throw new RenderBreakError('continue')
4+
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
5+
emitter.continue = true
66
}
77
}

src/builtin/tags/cycle.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export default {
2222
},
2323

2424
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
25-
const group = this.group.value(ctx)
25+
const group = ctx.sync
26+
? this.group.valueSync(ctx)
27+
: await this.group.value(ctx)
2628
const fingerprint = `cycle:${group}:` + this.candidates.join(',')
2729
const groups = ctx.getRegister('cycle')
2830
let idx = groups[fingerprint]
@@ -34,6 +36,9 @@ export default {
3436
const candidate = this.candidates[idx]
3537
idx = (idx + 1) % this.candidates.length
3638
groups[fingerprint] = idx
37-
emitter.write(new Expression(candidate).value(ctx))
39+
const html = ctx.sync
40+
? new Expression(candidate).valueSync(ctx)
41+
: await new Expression(candidate).value(ctx)
42+
emitter.write(html)
3843
}
3944
} as ITagImplOptions

0 commit comments

Comments
 (0)