File tree 7 files changed +29
-4
lines changed
test/integration/builtin/tags
7 files changed +29
-4
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ export default {
10
10
this . value = tokenizer . remaining ( )
11
11
} ,
12
12
render : function * ( ctx : Context ) {
13
- ctx . bottom ( ) [ this . key ] = yield this . liquid . _evalValue ( this . value , ctx )
13
+ const firstPageLevelScope = ctx . getFirstPageLevelScope ( )
14
+ firstPageLevelScope [ this . key ] = yield this . liquid . _evalValue ( this . value , ctx )
14
15
}
15
16
} as TagImplOptions
Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ export default {
34
34
const scope = yield hash . render ( ctx )
35
35
if ( withVar ) scope [ filepath ] = evalToken ( withVar , ctx )
36
36
const templates = yield liquid . _parsePartialFile ( filepath , ctx . sync , this [ 'currentFile' ] )
37
- ctx . push ( scope )
37
+ ctx . push ( { ... scope , isPageLevel : true } )
38
38
yield renderer . renderTemplates ( templates , ctx , emitter )
39
39
ctx . pop ( )
40
40
ctx . restoreRegister ( saved )
Original file line number Diff line number Diff line change @@ -56,7 +56,7 @@ export default {
56
56
const { value, alias } = this [ 'with' ]
57
57
scope [ alias || filepath ] = evalToken ( value , ctx )
58
58
}
59
- childCtx . push ( scope )
59
+ childCtx . push ( { ... scope , isPageLevel : true } )
60
60
61
61
if ( this [ 'for' ] ) {
62
62
const { value, alias } = this [ 'for' ]
Original file line number Diff line number Diff line change @@ -57,6 +57,11 @@ export class Context {
57
57
public bottom ( ) {
58
58
return this . scopes [ 0 ]
59
59
}
60
+ /** Returns the first scope that's considered to be a page-level scope, if one exists. Otherwise, defaults to `bottom`. */
61
+ public getFirstPageLevelScope ( ) {
62
+ const firstPageLevelScope = this . scopes . find ( ( scope : Scope ) => scope . isPageLevel )
63
+ return firstPageLevelScope || this . bottom ( )
64
+ }
60
65
private findScope ( key : string ) {
61
66
for ( let i = this . scopes . length - 1 ; i >= 0 ; i -- ) {
62
67
const candidate = this . scopes [ i ]
Original file line number Diff line number Diff line change @@ -5,4 +5,8 @@ export interface PlainObject {
5
5
toLiquid ?: ( ) => any ;
6
6
}
7
7
8
- export type Scope = PlainObject | Drop
8
+ export type Scope = ( PlainObject | Drop ) & {
9
+ /** Whether this is a page-level scope (e.g., corresponding to include/render/layout).
10
+ * Transient scopes (like those created by a for loop) are not considered to be page-level scopes. */
11
+ isPageLevel ?: boolean ;
12
+ }
Original file line number Diff line number Diff line change @@ -69,6 +69,11 @@ describe('tags/assign', function () {
69
69
const html = await liquid . parseAndRender ( src )
70
70
return expect ( html ) . to . equal ( '-6' )
71
71
} )
72
+ it ( 'should allow reassignment' , async function ( ) {
73
+ const src = '{% assign var = 1 %}{% assign var = 2 %}{{ var }}'
74
+ const html = await liquid . parseAndRender ( src )
75
+ return expect ( html ) . to . equal ( '2' )
76
+ } )
72
77
describe ( 'scope' , function ( ) {
73
78
it ( 'should read from parent scope' , async function ( ) {
74
79
const src = '{%for a in (1..2)%}{{num}}{%endfor%}'
Original file line number Diff line number Diff line change @@ -194,6 +194,16 @@ describe('tags/include', function () {
194
194
const html = await staticLiquid . renderFile ( 'parent.html' )
195
195
return expect ( html ) . to . equal ( 'Xchild with redY' )
196
196
} )
197
+
198
+ it ( 'should allow argument reassignment' , async function ( ) {
199
+ mock ( {
200
+ '/parent.html' : '{% include child.html, color: "red" %}' ,
201
+ '/child.html' : '{% assign color = "green" %}{{ color }}'
202
+ } )
203
+ const staticLiquid = new Liquid ( { dynamicPartials : false , root : '/' } )
204
+ const html = await staticLiquid . renderFile ( 'parent.html' )
205
+ return expect ( html ) . to . equal ( 'green' )
206
+ } )
197
207
} )
198
208
describe ( 'sync support' , function ( ) {
199
209
it ( 'should support quoted string' , function ( ) {
You can’t perform that action at this time.
0 commit comments