Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nymag/multiplex-templates
Easy embedding for multiple template languages
https://github.com/nymag/multiplex-templates
Last synced: 13 days ago
JSON representation
Easy embedding for multiple template languages
- Host: GitHub
- URL: https://github.com/nymag/multiplex-templates
- Owner: nymag
- License: mit
- Created: 2015-03-24T19:06:14.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2021-03-03T18:03:27.000Z (almost 4 years ago)
- Last Synced: 2024-12-14T10:19:23.815Z (27 days ago)
- Language: JavaScript
- Homepage:
- Size: 38.1 KB
- Stars: 2
- Watchers: 45
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# multiplex-templates
Embed components in other components![![Build Status](https://travis-ci.org/nymag/multiplex-templates.svg)](https://travis-ci.org/nymag/multiplex-templates)
[![Code Climate](https://codeclimate.com/github/nymag/multiplex-templates/badges/gpa.svg)](https://codeclimate.com/github/nymag/multiplex-templates)## Install
```
npm install --save multiplex-templates
```## Usage
### render a template
```js
var multiplex = require('multiplex-templates')();multiplex.render('path/to/template.ext', data);
```This will render a template. The templating engine it uses is determined by the template's extension.
e.g.
```
components/paragraph/template.jade
templates/header.nunjucks
```### Engines
We support [all of the engines that consolidate.js supports](https://github.com/tj/consolidate.js#supported-template-engines).
This module exposes the instances of the templating engines, so you can add mixins/filters/globals/etc onto them:
```js
var env = multiplex.engines.nunjucks;env.addGlobal('key', 'value');
```You can also instantiate your own engines (and configure them however you like) and pass them into multiplex-templates.
```js
var env = require('nunjucks').configure('.', { watch: false }),
jadeEnv = require('jade'), // so cool, doesn't need config (⌐■_■)
multiplex = require('multiplex-templates')({
nunjucks: env,
jade: jadeEnv
});// multiplex.engines.nunjucks === env
```## Cross-engine Embedding
To embed a template, call the `embed` function in the parent template, passing in the name of the template you want to embed, plus (optionally) data and defaults objects. The `embed` function is available in all templating languages that allow functions inside template locals.
**Nunjucks:**
```
{{ embed('path/to/tpl.nunjucks', data) | safe }}
```**Jade:**
```jade
section#foo
p.embedded
!= embed('path/to/tpl.jade', data)
```The `data` you pass in is then used to render the child template. You can optionally pass in additional data:
**Nunjucks:**
```
{{ embed('path/to/tpl.mustache', data, defaults) | safe }}
```**Jade:**
```jade
section#foo
p.embedded
!= embed('path/to/tpl.ejs', data, defaults)
```Properties in the `data` object will overwrite properties of the same name in the `defaults` object, as this uses lodash's fast `_.defaults()` method.
## Tests
```
npm test
```