Skip to content

Commit

Permalink
feat: globals shared between tags, see #185
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Feb 7, 2020
1 parent fc0cf6f commit 870e7ec
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
15 changes: 9 additions & 6 deletions src/context/context.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { Drop } from '../drop/drop'
import { __assign } from 'tslib'
import { assert } from '../util/assert'
import { NormalizedFullOptions, applyDefault } from '../liquid-options'
import { NormalizedFullOptions, defaultOptions } from '../liquid-options'
import { Scope } from './scope'
import { isArray, isNil, isString, isFunction, toLiquid } from '../util/underscore'

export class Context {
private scopes: Scope[] = [{}]
private registers = {}
public environments: Scope
public globals: Scope
public sync: boolean
public opts: NormalizedFullOptions
public constructor (env: object = {}, opts?: NormalizedFullOptions, sync = false) {
public constructor (env: object = {}, opts: NormalizedFullOptions = defaultOptions, sync = false) {
this.sync = sync
this.opts = applyDefault(opts)
this.opts = opts
this.globals = opts.globals
this.environments = env
}
public getRegister (key: string, defaultValue = {}) {
Expand All @@ -23,12 +25,12 @@ export class Context {
return (this.registers[key] = value)
}
public getAll () {
return [this.environments, ...this.scopes]
return [this.globals, this.environments, ...this.scopes]
.reduce((ctx, val) => __assign(ctx, val), {})
}
public get (path: string) {
const paths = this.parseProp(path)
const scope = this.findScope(paths[0]) || this.environments
const scope = this.findScope(paths[0])
return this.getFromScope(scope, paths)
}
public getFromScope (scope: object, paths: string[] | string) {
Expand Down Expand Up @@ -57,7 +59,8 @@ export class Context {
return candidate
}
}
return null
if (key in this.environments) return this.environments
return this.globals
}

/*
Expand Down
8 changes: 6 additions & 2 deletions src/liquid-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export interface LiquidOptions {
greedy?: boolean;
/** `fs` is used to override the default file-system module with a custom implementation. */
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 {
Expand All @@ -56,9 +58,10 @@ export interface NormalizedFullOptions extends NormalizedOptions {
outputDelimiterLeft: string;
outputDelimiterRight: string;
greedy: boolean;
globals: object;
}

const defaultOptions: NormalizedFullOptions = {
export const defaultOptions: NormalizedFullOptions = {
root: ['.'],
cache: false,
extname: '',
Expand All @@ -73,7 +76,8 @@ const defaultOptions: NormalizedFullOptions = {
outputDelimiterLeft: '{{',
outputDelimiterRight: '}}',
strictFilters: false,
strictVariables: false
strictVariables: false,
globals: {}
}

export function normalize (options?: LiquidOptions): NormalizedOptions {
Expand Down
13 changes: 13 additions & 0 deletions test/integration/builtin/tags/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ describe('tags/render', function () {
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 () {
mock({
'/with.html': '{% render "color" with "red", shape: "rect" %}',
Expand Down
2 changes: 1 addition & 1 deletion test/unit/parser/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ describe('parseStringLiteral()', function () {
it('should parse slash escape', () => {
expect(parseStringLiteral(String.raw`'fo\\o'`)).to.equal('fo\\o')
})
})
})

0 comments on commit 870e7ec

Please sign in to comment.