项目作者: jimlambie

项目描述 :
A Mustache.js interface for DADI Web
高级语言: JavaScript
项目地址: git://github.com/jimlambie/dadi-web-mustachejs.git
创建时间: 2017-08-10T08:41:55Z
项目社区:https://github.com/jimlambie/dadi-web-mustachejs

开源协议:

下载


DADI Web

Mustache.js engine interface

npm (scoped)
coverage
Build Status
JavaScript Style Guide
semantic-release

This module allows Mustache.js templates to be used with DADI Web.

Installation

  • Add this module as a dependency:

    1. npm install dadi-web-mustachejs --save
  • Include it in the engines array passed to Web:

    1. require('@dadi/web')({
    2. engines: [
    3. require('dadi-web-mustachejs')
    4. ]
    5. })

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:
    1. {
    2. {
    3. helpers: 'workspace/utils/helpers'
    4. }
    5. }

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.

  1. pages/
  2. |_ partials/
  3. |_ |_ common/
  4. |_ |_ |_ header.mustache
  5. |_ contact-info.mustache
  6. |_ 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:

  1. {{> 'partials/common/header' }}

Helpers

To use helpers supply the path to your helpers in the main Web configuration file:

  1. "engines": {
  2. "mustache": {
  3. "paths": {
  4. "helpers": "workspace/helpers"
  5. }
  6. }
  7. }

Helpers can be individual JavaScript files within the specifed directory, or all in a single file.

Example:

  1. /*
  2. * Returns the full name and price of the supplied product
  3. * The function receives the current context
  4. * Usage: {{ renderProduct }}
  5. */
  6. module.exports = function () {
  7. return `helper: ${this.name} - £${this.price}`
  8. }

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.

  1. {{#products}}
  2. <li>{{renderProduct}}</li>
  3. {{/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.