https://github.com/jimlambie/dadi-web-mustachejs
A Mustache.js interface for DADI Web
https://github.com/jimlambie/dadi-web-mustachejs
dadi dadi-web mustache mustache-js mustache-templates template-engine templates
Last synced: about 2 months ago
JSON representation
A Mustache.js interface for DADI Web
- Host: GitHub
- URL: https://github.com/jimlambie/dadi-web-mustachejs
- Owner: jimlambie
- Created: 2017-08-10T08:41:55.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-08-10T09:27:47.000Z (almost 8 years ago)
- Last Synced: 2025-03-04T01:11:57.656Z (3 months ago)
- Topics: dadi, dadi-web, mustache, mustache-js, mustache-templates, template-engine, templates
- Language: JavaScript
- Size: 17.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Mustache.js engine interface
[](https://www.npmjs.com/package/dadi-web-mustachejs)
[](https://github.com/jimlambie/dadi-web-mustachejs)
[](https://travis-ci.org/jimlambie/dadi-web-mustachejs)
[](http://standardjs.com/)
[](https://github.com/semantic-release/semantic-release)This module allows [Mustache.js](https://github.com/janl/mustache.js/) templates to be used with [DADI Web](https://github.com/dadi/web).
## Installation
- Add this module as a dependency:
```
npm install dadi-web-mustachejs --save
```- Include it in the `engines` array passed to Web:
```js
require('@dadi/web')({
engines: [
require('dadi-web-mustachejs')
]
})
```## Configuration
The following configuration parameters can be added to the global Web config file, under `engines.mustache`.
### `paths`
Paths required by Mustache.
- Format: `Object`
- Default:
```
{
{
helpers: 'workspace/utils/helpers'
}
}
```## Partials
Partials must be registered with Mustache before they can be used in a template. This library takes care of the registration for you, simply supply the path to your partials in the configuration option `additionalTemplates`.
```
pages/
|_ partials/
|_ |_ common/
|_ |_ |_ header.mustache
|_ contact-info.mustache
|_ home.mustache
```Partials are referenced by their relative path, minus the file extension. After loading the above hierarchy of templates and partials, to include `header.mustache` from the page `contact-info.mustache`, you would use the following syntax:
```mustache
{{> 'partials/common/header' }}
```## Helpers
To use helpers supply the path to your helpers in the main Web configuration file:
```json
"engines": {
"mustache": {
"paths": {
"helpers": "workspace/helpers"
}
}
}
```Helpers can be individual JavaScript files within the specifed directory, or all in a single file.
*Example:*
```js
/*
* Returns the full name and price of the supplied product
* The function receives the current context
* Usage: {{ renderProduct }}
*/
module.exports = function () {
return `helper: ${this.name} - £${this.price}`
}
```This function is now available in your templates, to be used as follows. The function receives the current context, in the following example it receives a `product` object with properties `name` and `price`.
```mustache
{{#products}}
{{/products}}
```
## More information
Read more in the Mustache documentation at https://github.com/janl/mustache.js
## Something missing?
Open a pull request or raise an issue.