bajo-template 2.5.3 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +19 -11
- package/package.json +1 -1
- package/wiki/CHANGES.md +6 -0
package/index.js
CHANGED
|
@@ -25,22 +25,22 @@ async function factory (pkgName) {
|
|
|
25
25
|
},
|
|
26
26
|
loopDetectorDur: '1m',
|
|
27
27
|
cache: {
|
|
28
|
-
|
|
28
|
+
ttlDur: '1s'
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
this.loopDetector = {}
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
buildCompileImports = (
|
|
34
|
+
buildCompileImports = (locals = {}, req = {}) => {
|
|
35
35
|
const _ = this.app.lib._
|
|
36
36
|
return {
|
|
37
37
|
_,
|
|
38
38
|
_t: (text, ...args) => {
|
|
39
|
-
const params = [...args, { lang }]
|
|
39
|
+
const params = [...args, { lang: req.lang }]
|
|
40
40
|
return this.t(text, ...params)
|
|
41
41
|
},
|
|
42
42
|
_format: (val, type, opts = {}) => {
|
|
43
|
-
opts.lang = opts.lang ?? lang
|
|
43
|
+
opts.lang = opts.lang ?? req.lang
|
|
44
44
|
return this.app.bajo.format(val, type, opts)
|
|
45
45
|
},
|
|
46
46
|
_findRoute: (input) => {
|
|
@@ -63,6 +63,14 @@ async function factory (pkgName) {
|
|
|
63
63
|
},
|
|
64
64
|
_dump: (value, noPre) => {
|
|
65
65
|
return (noPre ? '' : '<pre>') + JSON.stringify(value, null, 2) + (noPre ? '' : '</pre>')
|
|
66
|
+
},
|
|
67
|
+
_getSetting: (key, defValue) => {
|
|
68
|
+
if (!this.app.waibu) return _.get(locals, `schema.view.${key}`, defValue)
|
|
69
|
+
const cfg = this.app.waibu.getSetting(key, { defValue, req })
|
|
70
|
+
const { path } = this.app.bajo.breakNsPath(key)
|
|
71
|
+
const paths = path.replaceAll('/', '.').split('.')
|
|
72
|
+
if (paths[0] === '') paths.shift()
|
|
73
|
+
return _.get(locals, `schema.view.${paths.join('.')}`, cfg)
|
|
66
74
|
}
|
|
67
75
|
}
|
|
68
76
|
}
|
|
@@ -137,7 +145,7 @@ async function factory (pkgName) {
|
|
|
137
145
|
const { breakNsPath } = this.app.bajo
|
|
138
146
|
const start = '<!-- include '
|
|
139
147
|
const end = ' -->'
|
|
140
|
-
const imports = this.buildCompileImports(opts.
|
|
148
|
+
const imports = this.buildCompileImports(locals, opts.req)
|
|
141
149
|
while (content.includes(start) && content.includes(end)) {
|
|
142
150
|
const { pattern, result: rsc } = extractText(content, start, end)
|
|
143
151
|
if (!isEmpty(rsc)) {
|
|
@@ -170,7 +178,7 @@ async function factory (pkgName) {
|
|
|
170
178
|
_renderString = async (content, locals = {}, opts = {}) => {
|
|
171
179
|
const { merge, without, isString, omit, kebabCase, get } = this.app.lib._
|
|
172
180
|
if (opts.ext === '.md' && this.app.bajoMarkdown) {
|
|
173
|
-
content = await this.compile(content, locals, {
|
|
181
|
+
content = await this.compile(content, locals, { ttl: -1, req: opts.req }) // markdown can't process template tags, hence preprocess here
|
|
174
182
|
content = this.app.bajoMarkdown.parse(content)
|
|
175
183
|
}
|
|
176
184
|
let layout
|
|
@@ -207,7 +215,7 @@ async function factory (pkgName) {
|
|
|
207
215
|
const fullTitle = usePluginTitle ? `${locals.page.title} - ${this.app.waibuMpa.getPluginTitle(locals.page.ns, opts.lang)}` : locals.page.title
|
|
208
216
|
locals.page.fullTitle = locals.page.fullTitle ?? fullTitle
|
|
209
217
|
}
|
|
210
|
-
content = await this.compile(content, locals, {
|
|
218
|
+
content = await this.compile(content, locals, { ttl: this.config.cache.maxAgeDu, req: opts.req })
|
|
211
219
|
return await this._handleInclude(content, locals, opts)
|
|
212
220
|
}
|
|
213
221
|
|
|
@@ -238,12 +246,12 @@ async function factory (pkgName) {
|
|
|
238
246
|
return parseObject(success, { parseValue: false, lang }) ?? {}
|
|
239
247
|
}
|
|
240
248
|
|
|
241
|
-
compile = async (content, locals, {
|
|
249
|
+
compile = async (content, locals, { ttl = 0, req = {} } = {}) => {
|
|
242
250
|
const { get: getCache, set: setCache } = this.app.bajoCache ?? {}
|
|
243
251
|
const { template } = this.app.lib._
|
|
244
252
|
locals.attr = locals.attr ?? {}
|
|
245
253
|
const opts = {
|
|
246
|
-
imports: this.buildCompileImports(
|
|
254
|
+
imports: this.buildCompileImports(locals, req)
|
|
247
255
|
}
|
|
248
256
|
const key = 'fn:' + crypto.createHash('md5').update(content).digest('hex')
|
|
249
257
|
let item
|
|
@@ -270,7 +278,7 @@ async function factory (pkgName) {
|
|
|
270
278
|
let subNs
|
|
271
279
|
const isAbsolute = path.isAbsolute(tpl)
|
|
272
280
|
if (!isAbsolute) subNs = breakNsPath(tpl).subNs
|
|
273
|
-
const canCache = (isAbsolute || subNs === 'template') && this.config.cache !== false && cache
|
|
281
|
+
const canCache = (isAbsolute || subNs === 'template') && this.config.cache !== false && cache
|
|
274
282
|
if (canCache) {
|
|
275
283
|
const item = await cache.get({ key })
|
|
276
284
|
if (item) return item
|
|
@@ -279,7 +287,7 @@ async function factory (pkgName) {
|
|
|
279
287
|
let text = await this._render(tpl, locals, opts)
|
|
280
288
|
if (opts.postProcessor) text = await opts.postProcessor({ text, locals, opts })
|
|
281
289
|
if (subNs) await runHook(`${this.ns}:afterRender${upperFirst(subNs)}`, { tpl, locals, opts, text })
|
|
282
|
-
if (canCache) await cache.set({ key, value: text, ttl: opts.
|
|
290
|
+
if (canCache) await cache.set({ key, value: text, ttl: opts.ttlDur ?? this.config.cache.ttlDur })
|
|
283
291
|
return text
|
|
284
292
|
}
|
|
285
293
|
|
package/package.json
CHANGED
package/wiki/CHANGES.md
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
# Changes
|
|
2
2
|
|
|
3
|
+
## 2026-04-13
|
|
4
|
+
|
|
5
|
+
- [2.6.0] Add ```_getSetting()``` template function
|
|
6
|
+
- [2.6.0] ```buildCompileImports()``` now accept ```locals``` and ```req``` as its parameters
|
|
7
|
+
|
|
3
8
|
## 2026-03-25
|
|
4
9
|
|
|
5
10
|
- [2.5.3] Bug fix in cache handling for template function
|
|
11
|
+
- [2.5.4] Change ```maxAgeDur``` to ```ttlDur``` to align with bajoCache's terminology
|
|
6
12
|
|
|
7
13
|
## 2026-03-14
|
|
8
14
|
|