Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pdehaan/express-hbs-helpers-test
Testing global variables with Handlebars helpers.
https://github.com/pdehaan/express-hbs-helpers-test
Last synced: 7 days ago
JSON representation
Testing global variables with Handlebars helpers.
- Host: GitHub
- URL: https://github.com/pdehaan/express-hbs-helpers-test
- Owner: pdehaan
- License: mpl-2.0
- Created: 2020-02-06T18:55:51.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-11T23:35:34.000Z (about 2 years ago)
- Last Synced: 2024-12-19T15:07:05.215Z (17 days ago)
- Language: JavaScript
- Size: 56.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# express-hbs-helpers-test
Testing global variables with Handlebars helpers.
## WHY
Trying to see if we can inject global variables into Handlebars without needing to mess with passing values between templates.
## HOW
```js
const express = require("express");
const exphbs = require("express-handlebars");const app = express();
const hbs = exphbs.create({
extname: ".hbs",
defaultLayout: "default",
layoutsDir: path.join(__dirname, "views/layouts"),
partialsDir: path.join(__dirname, "views/partials"),
helpers: {
LOGO_ROOT: "https://logohost/foo/bar/",
HIBP_ROOT: "https://haveibeenpwned.com/api/v3/breaches"
}
});
```First, we're adding some global variables into the `helpers` object.
Now we can use the `LOGO_ROOT` and `HIBP_ROOT` constants in our templates, as seen in [views/home.hbs](views/home.hbs):
```hbs
{{!< default }}What, did this work?
in template:
{{ LOGO_ROOT }}
in partial:
{{> snippet }}
```You can use the global `{{ LOGO_ROOT }}` in our templates, or in any partials, as seen in [views/partials/snippet.hbs](views/partials/snippet.hbs):
```hbs
{{ HIBP_ROOT }}
```