fluid: Child Scope Does Not Modify Outer Scope Variables

When entering a child scope, variables defined in the parent scope can be read, but changes to their values do not propagate when the child scope is released. I would expect that changes made to a variable would persist to the outermost scope in which the variable is defined, is that correct? We are migrating to Fluid from DotLiquid and the behavior is different.

Here is an example (failing) unit test to demonstrate the issue, tested against Fluid v2.3.1:

        [Fact]
        public void ChildScope_Can_Modify_Outer_Scope_Variables()
        {
            var parser = new CustomParser();

            parser.RegisterExpressionBlock("repeat", async (value, statements, writer, encoder, context) =>
            {
                context.EnterChildScope();
                var repeatCount = (int)value.EvaluateAsync(context).Result.ToNumberValue();
                for (var i = 0; i < repeatCount; i++)
                {
                    await statements.RenderStatementsAsync(writer, encoder, context);
                }
                context.ReleaseScope();

                return Completion.Normal;
            });

            var template = parser.Parse("{% assign outer = 10 %}Outer={{ outer }}||{% repeat 3 %}{% assign outer = outer | plus: 1 %}Outer={{ outer }},{%- endrepeat %}||Outer={{ outer }}");
            var result = template.Render();

            Assert.Equal("Outer=10||Outer=11,Outer=12,Outer=13,||Outer=13", result);
        }

Expected Output: Outer=10||Outer=11,Outer=12,Outer=13,||Outer=13 Actual Output: Outer=10||Outer=11,Outer=12,Outer=13,||Outer=10

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 20 (10 by maintainers)

Most upvoted comments

I found some videos your community made about the migration from dotliquid -> fluid in Rocks. Some of the points that were made could be filed as bugs, you should not hesitate. I just published a PR fixing one of them (empty raw tags). Some others were explained as “different behavior” instead of explaining that this is to match the reference behavior of Shopify’s liquid, e.g. binary operator precedence, truncate words length, … Following the specs will help interoperability and documentation. I use their site to verify what’s a bug or not, even tried with this current issue to confirm it.