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

[Question] How to change root vars inside block helper

Open mruslan97 opened this issue 4 years ago • 1 comments

Hi all! I'm trying to write a block helper which fill context var. Root access example:

    handlebars.RegisterHelper("example", (output, options, context, arguments) =>  
    {  
	      count++;  
	      Console.WriteLine($"helper calls: {count}");  
	      var root = (JObject)options.Data["root"];  
	      root["someVar"] = root["someVar"].ToString() + count;

In this case 27 calls

But result is different

What is the reason and how do I fix this?

mruslan97 avatar Nov 24 '22 13:11 mruslan97

Handlebars templates are intentionally read-only with respect to context — you cannot set or mutate root variables from within a template or block helper. This matches Handlebars.js semantics. To work around this, compute all derived values in C# before rendering and include them in the context object you pass in.

rexm avatar Jun 20 '26 17:06 rexm