Handlebars.Net
Handlebars.Net copied to clipboard
[Question] How to change root vars inside block helper
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?
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.