Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add root context to data when using in-built #each and #which helpers #525

Closed
wants to merge 2 commits into from

Conversation

williamcoates
Copy link

Sometimes you need to access variables from the root context in any other sub-context. It can be very painful having to always figure out the right path back to the root context when in multiple loops.

For example in my app i have an i18n object bound to the root context, that my i18n helper needs to access. If I can't easily access the root context i need to workaround this by backing up the context like so

{{#with ../..}} {{i18n 'Foo'}{{/with}}

This is painful, and not always possible if the above code is in a partial which might be used from different contexts.

This patch simply adds the root context to the data hash in the #each and #which helpers, so you can build helpers which access the root context like so:

function(string, options) {
       return options.data.root.i18n.__(string);
}

It would be even cooler if you could do this in your templates:

{{@data.root.foo}}

But this would require altering the parser AFAIK which would require a bit more work...

Not sure on the etiquette for updating the distro, so I left those unchanged in this commit.

…ions.data.root when using base #each and #which helpers
@spikebrehm
Copy link
Contributor

Does this mean in templates you could use {{@root}}?

@williamcoates
Copy link
Author

No, its just adds root to the context so your helpers can use it

@BrewDawg
Copy link

BrewDawg commented Oct 6, 2013

You guys are off your game, it's easy ...

I have created one see http://www.my2ndgeneration.com/TemplateLanguageDoc.aspx#xroot

Basically, add this helper and bingo {{xRoot}} will take you to the top ...

I always pass my JSON data into handlebars like this:

{ data: self.data } 

thus the code below always returns "data" when it sees the xRoot tag and takes me to the top

Handlebars.JavaScriptCompiler.prototype.nameLookup = function (parent, name, type) {

    if (name.indexOf("xRoot") === 0) {
        return "data";
    }

    if (/^[0-9]+$/.test(name)) {
        return parent + "[" + name + "]";
    } else if (Handlebars.JavaScriptCompiler.isValidJavaScriptVariableName(name)) {
        return parent + "." + name;
    }
    else {
        return parent + "['" + name + "']";
    }
};

By the way, I've built a tool that allows you to execute full Handlebar templates on the desktop, without a browser, it's pretty cool

See http://www.tiraggo.com/movies/M2G_Desktop_Reveal.html

@jonschlinkert
Copy link
Contributor

👍 would be nice to see this merged

@kpdecker
Copy link
Collaborator

Thanks for the PR and sorry it took me so long to look at this. We're going to do something like this but I want it to be generically accessible rather than just within these helpers. I've attached a PR to #392 to this end.

@kpdecker kpdecker closed this Jan 15, 2014
@BrewDawg
Copy link

So how would use use this? I guess it's @root ? I would have thought you would have been {{root}} or {{root.Items}}

Can you show us how you would use root to get back up to the root of the data and access some properties?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants