mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
feat: globals shared between tags, see #185
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { Drop } from '../drop/drop'
|
import { Drop } from '../drop/drop'
|
||||||
import { __assign } from 'tslib'
|
import { __assign } from 'tslib'
|
||||||
import { assert } from '../util/assert'
|
import { assert } from '../util/assert'
|
||||||
import { NormalizedFullOptions, applyDefault } from '../liquid-options'
|
import { NormalizedFullOptions, defaultOptions } from '../liquid-options'
|
||||||
import { Scope } from './scope'
|
import { Scope } from './scope'
|
||||||
import { isArray, isNil, isString, isFunction, toLiquid } from '../util/underscore'
|
import { isArray, isNil, isString, isFunction, toLiquid } from '../util/underscore'
|
||||||
|
|
||||||
@@ -9,11 +9,13 @@ export class Context {
|
|||||||
private scopes: Scope[] = [{}]
|
private scopes: Scope[] = [{}]
|
||||||
private registers = {}
|
private registers = {}
|
||||||
public environments: Scope
|
public environments: Scope
|
||||||
|
public globals: Scope
|
||||||
public sync: boolean
|
public sync: boolean
|
||||||
public opts: NormalizedFullOptions
|
public opts: NormalizedFullOptions
|
||||||
public constructor (env: object = {}, opts?: NormalizedFullOptions, sync = false) {
|
public constructor (env: object = {}, opts: NormalizedFullOptions = defaultOptions, sync = false) {
|
||||||
this.sync = sync
|
this.sync = sync
|
||||||
this.opts = applyDefault(opts)
|
this.opts = opts
|
||||||
|
this.globals = opts.globals
|
||||||
this.environments = env
|
this.environments = env
|
||||||
}
|
}
|
||||||
public getRegister (key: string, defaultValue = {}) {
|
public getRegister (key: string, defaultValue = {}) {
|
||||||
@@ -23,12 +25,12 @@ export class Context {
|
|||||||
return (this.registers[key] = value)
|
return (this.registers[key] = value)
|
||||||
}
|
}
|
||||||
public getAll () {
|
public getAll () {
|
||||||
return [this.environments, ...this.scopes]
|
return [this.globals, this.environments, ...this.scopes]
|
||||||
.reduce((ctx, val) => __assign(ctx, val), {})
|
.reduce((ctx, val) => __assign(ctx, val), {})
|
||||||
}
|
}
|
||||||
public get (path: string) {
|
public get (path: string) {
|
||||||
const paths = this.parseProp(path)
|
const paths = this.parseProp(path)
|
||||||
const scope = this.findScope(paths[0]) || this.environments
|
const scope = this.findScope(paths[0])
|
||||||
return this.getFromScope(scope, paths)
|
return this.getFromScope(scope, paths)
|
||||||
}
|
}
|
||||||
public getFromScope (scope: object, paths: string[] | string) {
|
public getFromScope (scope: object, paths: string[] | string) {
|
||||||
@@ -57,7 +59,8 @@ export class Context {
|
|||||||
return candidate
|
return candidate
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null
|
if (key in this.environments) return this.environments
|
||||||
|
return this.globals
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ export interface LiquidOptions {
|
|||||||
greedy?: boolean;
|
greedy?: boolean;
|
||||||
/** `fs` is used to override the default file-system module with a custom implementation. */
|
/** `fs` is used to override the default file-system module with a custom implementation. */
|
||||||
fs?: IFS;
|
fs?: IFS;
|
||||||
|
/** the global environment passed down to all partial templates, i.e. templates included by `include`, `layout` and `render` tags. */
|
||||||
|
globals?: object;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface NormalizedOptions extends LiquidOptions {
|
interface NormalizedOptions extends LiquidOptions {
|
||||||
@@ -56,9 +58,10 @@ export interface NormalizedFullOptions extends NormalizedOptions {
|
|||||||
outputDelimiterLeft: string;
|
outputDelimiterLeft: string;
|
||||||
outputDelimiterRight: string;
|
outputDelimiterRight: string;
|
||||||
greedy: boolean;
|
greedy: boolean;
|
||||||
|
globals: object;
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultOptions: NormalizedFullOptions = {
|
export const defaultOptions: NormalizedFullOptions = {
|
||||||
root: ['.'],
|
root: ['.'],
|
||||||
cache: false,
|
cache: false,
|
||||||
extname: '',
|
extname: '',
|
||||||
@@ -73,7 +76,8 @@ const defaultOptions: NormalizedFullOptions = {
|
|||||||
outputDelimiterLeft: '{{',
|
outputDelimiterLeft: '{{',
|
||||||
outputDelimiterRight: '}}',
|
outputDelimiterRight: '}}',
|
||||||
strictFilters: false,
|
strictFilters: false,
|
||||||
strictVariables: false
|
strictVariables: false,
|
||||||
|
globals: {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function normalize (options?: LiquidOptions): NormalizedOptions {
|
export function normalize (options?: LiquidOptions): NormalizedOptions {
|
||||||
|
|||||||
@@ -76,6 +76,19 @@ describe('tags/render', function () {
|
|||||||
return expect(html).to.equal('InParent: harttle InChild: ')
|
return expect(html).to.equal('InParent: harttle InChild: ')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should be able to access globals', async function () {
|
||||||
|
mock({
|
||||||
|
'/hash.html': 'InParent: {{name}} {% render "user.html" %}',
|
||||||
|
'/user.html': 'InChild: {{name}}'
|
||||||
|
})
|
||||||
|
const html = await liquid.renderFile('hash.html', {
|
||||||
|
name: 'harttle'
|
||||||
|
}, {
|
||||||
|
globals: { name: 'Harttle' }
|
||||||
|
})
|
||||||
|
return expect(html).to.equal('InParent: harttle InChild: Harttle')
|
||||||
|
})
|
||||||
|
|
||||||
it('should support render: with', async function () {
|
it('should support render: with', async function () {
|
||||||
mock({
|
mock({
|
||||||
'/with.html': '{% render "color" with "red", shape: "rect" %}',
|
'/with.html': '{% render "color" with "red", shape: "rect" %}',
|
||||||
|
|||||||
Reference in New Issue
Block a user