Handlebars.Net icon indicating copy to clipboard operation
Handlebars.Net copied to clipboard

Template Section fails to render variable within when using dot-Paths

Open schickst opened this issue 6 years ago • 3 comments

Hi,

I noticed some weird behaviour when using sections.

When I use {{#InCa}} the {{data.client.name}}-Variable within can be rendered. If I use a dotted path as a section the behaviour is different. I used {{#parameter.letterpaper_S}} and the {{data.client.name}}-Variable within wasn't rendered.

Template:

{{#InCa}}
    Test 1
    {{data.client.name}}<br/>
 {{/InCa}}
 {{#parameter.letterpaper_S}}
    Test 2
    {{data.client.name}}
{{/parameter.letterpaper_S}}

Output:

 Test 1
 STEC<br/>
 Test 2

My expectations would be, that the {{data.client.name}}-variable can be rendered in both cases.

schickst avatar Dec 29 '19 22:12 schickst

What does the input data model look like?

rexm avatar Dec 30 '19 15:12 rexm

Thanks for the enlightening question. The input model is added below. I did some more digging and tried to narrow it down. Instead I found, that the issue isn't the dot-syntax. The big difference seems to be the type of the object in the input model. Once I provided "True" as string and once True as bool. The string version seems to cause the trouble.

To validate my theory I added a third test. All tests used the input model below.

Template:

{{#data.logo}}
    Test 3
    {{data.client.name}}
{{/data.logo}}

Output:

   Test 3
    

Input Data model:

var data = new Dictionary<string, object>()
            {
                { "data", new Dictionary<string, object>()
                    {
                        { "client", new Dictionary<string, object>()
                            {
                                { "name", "STEC" },
                            }
                        },
                        { "logo", "somethingFooBar"}
                    }
                },
                { "parameter", new Dictionary<string, object>()
                    {
                        { "letterpaper_S", "True" },
                    }
                },
                { "InCa", true },
            };

So I currently see two open questions:

  1. Why doesn't {{data.client.name}} get rendered in each block?
  2. Is the .net Type system too strict in the evaluation of section true/false evaluations? As the template was already used successfully in a JavaScript-Context, my current theory is that Handlebars.Net behaves different because of the type system. Javascript uses a "truthy" (Javascript ==-operator) evaluation for the sections. I am not sure if this point is a bug or a feature. A problem in the section evaluation (e.g. a supressed exception) could also be the reason for point 1.

schickst avatar Dec 30 '19 19:12 schickst

I looked closer at your template, and it's not clear to me what you're trying to do. #data.logo is not valid syntax - the # prefix indicates a block helper name. So the block isn't valid to begin with. Another issue is when a block is created, the implicit root of that block's data scope is the model for the block. So even if #data.logo was a valid way to create a block, the data context inside would be relative to data.logo, which means for the {{data.client.name}} to work, logo would need to have a child property data which in turn have child properties client and respectively, name.

I suggest you read up on the Handlebars documentation - they do a better job explaining the basics than I can here.

Edit: to answer your question about the .NET type system, Handlebars.Net has its own implementation to imitate Javascript's rules for truthiness.

rexm avatar Jan 02 '20 22:01 rexm