mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
fix: allow {%render%} to reassign argument, #404
This commit is contained in:
committed by
Harttle
parent
432d1c7ccb
commit
124f4c4485
@@ -441,6 +441,15 @@
|
||||
"contributions": [
|
||||
"doc"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "AleksandrHovhannisyan",
|
||||
"name": "Aleksandr Hovhannisyan",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/19352442?v=4",
|
||||
"profile": "https://www.aleksandrhovhannisyan.com/",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
}
|
||||
],
|
||||
"contributorsPerLine": 7,
|
||||
|
||||
@@ -112,6 +112,7 @@ Want to contribute? see [Contribution Guidelines][contribution]. Thanks goes to
|
||||
<td align="center"><a href="https://github.com/bglw"><img src="https://avatars.githubusercontent.com/u/40188355?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Liam Bigelow</b></sub></a><br /><a href="https://github.com/harttle/liquidjs/commits?author=bglw" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://about.me/jasonkurian"><img src="https://avatars.githubusercontent.com/u/2642545?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jason Kurian</b></sub></a><br /><a href="https://github.com/harttle/liquidjs/commits?author=JaKXz" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://github.com/dphm"><img src="https://avatars.githubusercontent.com/u/1707217?v=4?s=100" width="100px;" alt=""/><br /><sub><b>d pham (they/them)</b></sub></a><br /><a href="https://github.com/harttle/liquidjs/commits?author=dphm" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://www.aleksandrhovhannisyan.com/"><img src="https://avatars.githubusercontent.com/u/19352442?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Aleksandr Hovhannisyan</b></sub></a><br /><a href="https://github.com/harttle/liquidjs/commits?author=AleksandrHovhannisyan" title="Code">💻</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { __assign } from 'tslib'
|
||||
import { assert } from '../../util/assert'
|
||||
import { ForloopDrop } from '../../drop/forloop-drop'
|
||||
import { toEnumerable } from '../../util/collection'
|
||||
@@ -51,12 +52,12 @@ export default {
|
||||
assert(filepath, () => `illegal filename "${filepath}"`)
|
||||
|
||||
const childCtx = new Context({}, ctx.opts, ctx.sync)
|
||||
const scope = yield hash.render(ctx)
|
||||
const scope = childCtx.bottom()
|
||||
__assign(scope, yield hash.render(ctx))
|
||||
if (this['with']) {
|
||||
const { value, alias } = this['with']
|
||||
scope[alias || filepath] = evalToken(value, ctx)
|
||||
}
|
||||
childCtx.push(scope)
|
||||
|
||||
if (this['for']) {
|
||||
const { value, alias } = this['for']
|
||||
@@ -67,7 +68,7 @@ export default {
|
||||
scope[alias] = item
|
||||
const templates = yield liquid._parsePartialFile(filepath, childCtx.sync, this['currentFile'])
|
||||
yield liquid.renderer.renderTemplates(templates, childCtx, emitter)
|
||||
scope.forloop.next()
|
||||
scope['forloop'].next()
|
||||
}
|
||||
} else {
|
||||
const templates = yield liquid._parsePartialFile(filepath, childCtx.sync, this['currentFile'])
|
||||
|
||||
@@ -6,9 +6,21 @@ import { isArray, isNil, isString, isFunction, toLiquid } from '../util/undersco
|
||||
import { InternalUndefinedVariableError } from '../util/error'
|
||||
|
||||
export class Context {
|
||||
/**
|
||||
* insert a Context-level empty scope,
|
||||
* for tags like {% capture %} {% assign %} to operate
|
||||
*/
|
||||
private scopes: Scope[] = [{}]
|
||||
private registers = {}
|
||||
/**
|
||||
* user passed in scope
|
||||
* {% increment %}, {% decrement %} changes this scope,
|
||||
* whereas {% capture %}, {% assign %} only hide this scope
|
||||
*/
|
||||
public environments: Scope
|
||||
/**
|
||||
* global scope used as fallback for missing variables
|
||||
*/
|
||||
public globals: Scope
|
||||
public sync: boolean
|
||||
public opts: NormalizedFullOptions
|
||||
|
||||
@@ -55,7 +55,7 @@ export interface LiquidOptions {
|
||||
greedy?: boolean;
|
||||
/** `fs` is used to override the default file-system module with a custom implementation. */
|
||||
fs?: FS;
|
||||
/** the global environment passed down to all partial templates, i.e. templates included by `include`, `layout` and `render` tags. */
|
||||
/** the global scope passed down to all partial and layout templates, i.e. templates included by `include`, `layout` and `render` tags. */
|
||||
globals?: object;
|
||||
/** Whether or not to keep value type when writing the Output, not working for streamed rendering. Defaults to `false`. */
|
||||
keepOutputType?: boolean;
|
||||
|
||||
@@ -69,6 +69,11 @@ describe('tags/assign', function () {
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal('-6')
|
||||
})
|
||||
it('should allow reassignment', async function () {
|
||||
const src = '{% assign var = 1 %}{% assign var = 2 %}{{ var }}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal('2')
|
||||
})
|
||||
describe('scope', function () {
|
||||
it('should read from parent scope', async function () {
|
||||
const src = '{%for a in (1..2)%}{{num}}{%endfor%}'
|
||||
|
||||
@@ -19,7 +19,7 @@ describe('tags/capture', function () {
|
||||
return expect(html).to.equal('A')
|
||||
})
|
||||
|
||||
it('should shading rather than overwriting', async function () {
|
||||
it('should not change root scope', async function () {
|
||||
const src = '{% capture var %}10{% endcapture %}{{var}}'
|
||||
const ctx = { 'var': 20 }
|
||||
const html = await liquid.parseAndRender(src, ctx)
|
||||
|
||||
@@ -39,7 +39,7 @@ describe('tags/increment', function () {
|
||||
return expect(html).to.equal('012 10')
|
||||
})
|
||||
|
||||
it('should not shading capture', async function () {
|
||||
it('should not hide capture', async function () {
|
||||
const src = '{% capture var %}10{% endcapture %}{% increment var %}{% increment var %}{% increment var %} {{var}}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal('012 10')
|
||||
|
||||
@@ -86,6 +86,16 @@ describe('tags/render', function () {
|
||||
expect(html).to.equal('InParent: harttle InChild: ')
|
||||
})
|
||||
|
||||
it('should allow argument reassignment', async function () {
|
||||
mock({
|
||||
'/parent.html': '{% render child.html, color: "red" %}',
|
||||
'/child.html': '{% assign color = "green" %}{{ color }}'
|
||||
})
|
||||
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
|
||||
const html = await staticLiquid.renderFile('parent.html')
|
||||
return expect(html).to.equal('green')
|
||||
})
|
||||
|
||||
it('should be able to access globals', async function () {
|
||||
liquid = new Liquid({ root: '/', extname: '.html', globals: { name: 'Harttle' } })
|
||||
mock({
|
||||
|
||||
Reference in New Issue
Block a user