Skip to content

Commit

Permalink
fix: allow {%render%} to reassign argument, #404
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrHovhannisyan authored and harttle committed Oct 31, 2021
1 parent 432d1c7 commit 124f4c4
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 6 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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>

Expand Down
7 changes: 4 additions & 3 deletions src/builtin/tags/render.ts
@@ -1,3 +1,4 @@
import { __assign } from 'tslib'
import { assert } from '../../util/assert'
import { ForloopDrop } from '../../drop/forloop-drop'
import { toEnumerable } from '../../util/collection'
Expand Down Expand Up @@ -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']
Expand All @@ -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'])
Expand Down
12 changes: 12 additions & 0 deletions src/context/context.ts
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/liquid-options.ts
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions test/integration/builtin/tags/assign.ts
Expand Up @@ -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%}'
Expand Down
2 changes: 1 addition & 1 deletion test/integration/builtin/tags/capture.ts
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/builtin/tags/increment.ts
Expand Up @@ -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')
Expand Down
10 changes: 10 additions & 0 deletions test/integration/builtin/tags/render.ts
Expand Up @@ -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({
Expand Down

0 comments on commit 124f4c4

Please sign in to comment.