You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Extend the handlers with an direct access to @root over their parameters #322
I used this to access the main data structure from the calling in the handler functions. Before this, the data structure have to be "public static" or the scripts must have an weird @root parameter.
I needed handlers that have access to the calling data structure to access the context of the entire execution.
My first approach was that the handler always had to be given a @root parameter that had to be entered in the HBS scripts.
E.G. {{fontsize @root 6}}
because the fontsize referred to the instance. However, this makes little sense from the point of view of the script, which is why I prefer a {{fontsize 6}}. This is with my change also possible because EVERY handler has direct access to the @root instance and thus the scripts are freed of unnecessary payload.
Example: (GetTicksCounter() is the "environment")
class RootData
{
public int Number { get; set; }
public int GetTicksCounter() => new Random().Next(17, 42);
}
public void BlockHelperRootAccess()
{
var source = "...{{testhandler 1}}...";
var rootTestData = new RootData
{
Number = 17
};
Handlebars.RegisterHelper("testhandler", (writer, root, context, arguments) => {
Assert.True(root == rootTestData);
var rootData = root as RootData;
writer.Write(int.Parse(arguments[0].ToString()) * rootData.GetTicksCounter());
});
var template = Handlebars.Compile(source);
var output = template(rootTestData);
Willing to consider it for v2, but want to put a little more thought into how to achieve it with closer alignment to how the JS library logically works.
Activity
rexm commentedon Nov 10, 2019
Not clear to me what you’re asking, can you elaborate?
GitHub-TC commentedon Nov 10, 2019
I needed handlers that have access to the calling data structure to access the context of the entire execution.
My first approach was that the handler always had to be given a @root parameter that had to be entered in the HBS scripts.
E.G. {{fontsize @root 6}}
because the fontsize referred to the instance. However, this makes little sense from the point of view of the script, which is why I prefer a {{fontsize 6}}. This is with my change also possible because EVERY handler has direct access to the @root instance and thus the scripts are freed of unnecessary payload.
Example: (GetTicksCounter() is the "environment")
Here's my fork:
https://github.com/GitHub-TC/Handlebars.Net
and i used this in the
https://github.com/GitHub-TC/EmpyrionScripting
rexm commentedon Nov 12, 2019
Handlebarsjs doesn't support this today - helpers are limited to the data scope they have access to.
GitHub-TC commentedon Nov 12, 2019
I know that, and I changed my (fork) version to do that.
My question is if you are interested in this change?
rexm commentedon Nov 13, 2019
Willing to consider it for v2, but want to put a little more thought into how to achieve it with closer alignment to how the JS library logically works.
brechtvhb commentedon May 31, 2024
Is it already possible to access the root context from a helper without having to inject @root?