Skip to content

Way to access complete data object from helper #320

Closed
dotnet/arcade
#16884

Description

@huysentruitw

I'm trying to get a translation helper working and I'm pretty close:

In the template, I'm writing:

{{ translate "Hello world" }}

Then I have a helper:

_handlebars.RegisterHelper("translate", new HandlebarsHelper((writer, data, obj) =>
{
    if (obj.Length != 1 || !(obj[0] is string caption))
        return;

    if (data is HandlebarsCultureSpecificTemplateData tempateData)
    {
        caption = tempateData.Translate?.Invoke(caption) ?? caption;
    }

    writer.Write(caption);
}));

The class looks like this:

internal class HandlebarsCultureSpecificTemplateData
{
    public Func<string, string> Translate { get; set; }
    public CultureInfo Culture { get; set; }
}

Now, this works, until I get into an {{#each}} loop, there data in the helper no longer points to the root object, but points to the current element of the 'each'.

So, my question: how to access the root object from the helper (in all cases) OR how can I execute the helper from the template, but making sure it's getting called from the root scope? (like @root)

Activity

  1. huysentruitw commented on Oct 23, 2019

    @huysentruitw
    ContributorAuthor

    Currently changed our usage to: {{ translate @root "Hello world" }}

    And helper to:

    _handlebars.RegisterHelper("translate", new HandlebarsHelper((writer, data, obj) =>
    {
        if (obj.Length != 2 || !(obj[1] is string caption))
            return;
    
        if (obj[0] is HandlebarsCultureSpecificTemplateData tempateData)
        {
            caption = tempateData.Translate?.Invoke(caption) ?? caption;
        }
    
        writer.Write(caption);
    }));

    A cleaner way would be nice :)

  2. rexm commented on Oct 23, 2019

    @rexm
    Member

    How does it work in handlebars js?

  3. huysentruitw commented on Oct 23, 2019

    @huysentruitw
    ContributorAuthor

    In handlebars js, they're passing the options instance to the helper callback, where options.data contains the raw data that is being feeded to the compile function.

    F.e. https://github.com/wycats/handlebars.js/blob/148b19182d70278237a62d8293db540483a0c46c/lib/handlebars/helpers/with.js

  4. rexm commented on Oct 29, 2019

    @rexm
    Member

    @huysentruitw To make sure we've got the use case nailed down, can you please share a simple example template & helper written in handlebars js, with an example JSON model?

  5. added this to the Release 2.0 milestone on Oct 28, 2020
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions