Skip to content

Extend the handlers with an direct access to @root over their parameters #322

Closed
dotnet/arcade
#16884

Description

@GitHub-TC
  • public delegate void HandlebarsHelper (TextWriter output, object root, dynamic context, params object[] arguments);
  • public delegate void HandlebarsBlockHelper(TextWriter output, object root, HelperOptions options, dynamic context, params object[] arguments);

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.

Activity

  1. rexm commented on Nov 10, 2019

    @rexm
    Member

    Not clear to me what you’re asking, can you elaborate?

  2. GitHub-TC commented on Nov 10, 2019

    @GitHub-TC
    Author

    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);
    

    Here's my fork:
    https://github.com/GitHub-TC/Handlebars.Net

    and i used this in the
    https://github.com/GitHub-TC/EmpyrionScripting

  3. rexm commented on Nov 12, 2019

    @rexm
    Member

    Handlebarsjs doesn't support this today - helpers are limited to the data scope they have access to.

  4. GitHub-TC commented on Nov 12, 2019

    @GitHub-TC
    Author

    I know that, and I changed my (fork) version to do that.
    My question is if you are interested in this change?

  5. rexm commented on Nov 13, 2019

    @rexm
    Member

    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.

  6. brechtvhb commented on May 31, 2024

    @brechtvhb

    Is it already possible to access the root context from a helper without having to inject @root?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions