Giter Club home page Giter Club logo

Comments (14)

defunctzombie avatar defunctzombie commented on August 12, 2024

I would ideally like to see some simple example app with code showing the desired result and why it can't currently be done (or what the advantage is to changing).

from hbs.

scottgonzalez avatar scottgonzalez commented on August 12, 2024

Ok, I'll put together an example app tonight. But to explain the desired behavior, I'd like to be able to do:

web.get( "/", function( request, response ) {
    response.locals.foo = "bar";
    response.render( "page", { items: [
        { label: "a" },
        { label: "b" }
    ] } );
});

With page.hbs being:

top level: {{@foo}}
{{#each items}}
    {{label}}: {{@foo}}
{{/each}}

And have the output be:

top level: bar
a: bar
b: bar

Essentially, I want app.locals and response.locals to be treated as globals within the templates.

from hbs.

scottgonzalez avatar scottgonzalez commented on August 12, 2024

I've created https://github.com/scottgonzalez/hbs-lexical-data.

In app.js, you'll see that I proxy app.render() to store the lexical data. I also set some data in app.locals and response.locals.

The relevant change within hbs is the passing of the lexical data to the template. You can see that within the view, I can reference this data even after a context change, by using @property.

from hbs.

scottgonzalez avatar scottgonzalez commented on August 12, 2024

Have you had a chance to think about whether something like this could be supported? I'd like to move forward with this in my project, but I don't want to drop hbs if this might land.

from hbs.

defunctzombie avatar defunctzombie commented on August 12, 2024

Will try to look at this today. At first glance I would have thought this
was already supported.
On Feb 5, 2014 10:13 AM, "Scott González" [email protected] wrote:

Have you had a chance to think about whether something like this could be
supported? I'd like to move forward with this in my project, but I don't
want to drop hbs if this might land.

Reply to this email directly or view it on GitHubhttps://github.com//issues/57#issuecomment-34185109
.

from hbs.

scottgonzalez avatar scottgonzalez commented on August 12, 2024

I think the reason it's not supported is because Express doesn't provide a clean way to implement this. All three data sources get merged together before being passed to the view and the view doesn't have a reference to the response object to find out what local data exists.

from hbs.

scottgonzalez avatar scottgonzalez commented on August 12, 2024

Have you had a chance to look at this?

from hbs.

scottgonzalez avatar scottgonzalez commented on August 12, 2024

bump

from hbs.

defunctzombie avatar defunctzombie commented on August 12, 2024

I took a look. Seems that handlebars calls them "private" variables (http://handlebarsjs.com/execution.html). I would be up for passing them along using the _data local approach you implemented. I wish there was a way to actually pass separate options, but there doesn't seem to be.

Would accept a PR with tests and documentation update about the special use of a _data local.

from hbs.

scottgonzalez avatar scottgonzalez commented on August 12, 2024

Thanks. I'll put together a patch in a few days.

from hbs.

jas avatar jas commented on August 12, 2024

@scottgonzalez I had no idea Handlebars allowed you to define custom "global" variables. There's a few places that I would like to use this in my Express app. Have you done any work on a pull request?

from hbs.

scottgonzalez avatar scottgonzalez commented on August 12, 2024

Not yet. @defunctzombie gave me the go ahead right when I was getting really busy. I was hoping I'd get it done that week before my schedule got crazy, but that didn't happen. I just got back into the project that needs this over the weekend. Once I finish my current task in that project, I'll be working on this. If you have time to work on this right now, go ahead.

from hbs.

scottgonzalez avatar scottgonzalez commented on August 12, 2024

@defunctzombie I'm ready to implement this now. Since we have to override app.render(), I'd like to figure out what the API should be. We can either require("express") and change the app prototype automatically, or we can expose a function for applying this behavior to an instance. That could be done either through app.use(hbs.something()) (which has the benefit of following common convention, but unnecessarily runs on every request), or through hbs.something(app).

from hbs.

defunctzombie avatar defunctzombie commented on August 12, 2024

This is now migrated to PR #63

from hbs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.