Comments (14)
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.
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.
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.
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.
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.
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.
Have you had a chance to look at this?
from hbs.
bump
from hbs.
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.
Thanks. I'll put together a patch in a few days.
from hbs.
@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.
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.
@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.
This is now migrated to PR #63
from hbs.
Related Issues (20)
- Update handlebars dependency to 4.1.0 HOT 1
- [Security Alert] Upgrade handlebars to 4.1.0 or later HOT 1
- high severity vulnerability HOT 1
- Partial's passing value don't rendering after a space, in express hbs HOT 2
- Access hbs variable in external js file HOT 1
- npm audit security issue with handlebars 4.0.x HOT 8
- Error: Failed to lookup view "index" in views directory - Where is problem? HOT 4
- NPM Audit Security Issue with handlebars HOT 2
- Handlebars: Access has been denied to resolve the property "statements" because it is not an "own property" of its parent. HOT 3
- Handlebars 4.7.3 HOT 1
- Low security on minimist HOT 3
- Outdated dependency 'minimist' HOT 3
- Using registePartials twice disables app.locals HOT 3
- Can i use multiple videv engines with this framework? HOT 1
- Is it possible to pass an object(or JSON) from express to a specific partial(for use by another contained js app)? HOT 1
- Security Contact Needed HOT 2
- css file problem. HOT 1
- CSS not loaded when more than one '/' in the path HOT 1
- Error trying to use hbs on express
- How can I clear the cache? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hbs.