A content compiler on Node.js
ConcoctJS knows how to compile templates with some context and write the results to disk. Anything in the middle is up to plugins.
ConcoctJS works with any templating engine, as long as there is a plugin to handle it. The contexts with which templates are compiled are simply JSON files; and they too can be modified by plugins. In fact, JSON files are not really needed, as contexts can be generated just by plugins.
Contexts are linked to templates using one of the following:
- Linking rules.
- Plugin(s).
- Combination of (1) and (2).
ConcoctJS' workflow is simple:
- Load templates and contexts from disk.
- Link contexts to templates.
- Run plugins.
- Write compiled buffers to disk.
If no plugin is present to actually compile the templates, then all the buffers are just copies of the uncompiled templates.
The ConcoctJS module exports a constructor which receives an options object with the following fields:
The constructed object has only one public method - concoct().
Type: String / Array
Required: yes
Tells Concoct where to find templates. Can be specified as a glob pattern string, or an array of such strings.
{
templates: [
"article_templates/*.tpl",
"static_templates/*.tpl",
"other_templates/**/*.tpl"
]
}Type: String / Array
Required: yes
Tells Concoct where to find contexts.
Works the same as the templates field (see above).
Type: Array
Required: no
An array of 'plugin' objects. See the section 'Plugins' below.
Type: Object
Required: no
A collection of key:value pairs, which are used to associate contexts with templates. both 'key' and 'value' can be path glob pattens.
Linking rules are defined with a glob pattern matching dictionary. So from x templates and y contexts you can compile (at most) x * y files.
{
"contexts/*.json": "templates/*.tpl"
}{
"contexts/*.json": "templates/article.tpl"
}Type: String
Required: no
Destination directory to write the buffers.
The concoct method starts the concoction process.
A 'plugin' object contains the information needed by Concoct to run the plugin. It needs to have to following fields (all are optional)
Type: String
The name of the plugin as to be identified in Concoct's log messages.
Type: Function
Arguments: params, templates, contexts, links, buffers, done
The function Concoct calls when running the plugin.
Type: Object
An object to be passed to the plugin's handler function.
npm install concoct
var Concoction = require('concoct'),
options = {
plugins: [ /* list of plugins */ ],
templates: [ /* paths to templates */ ],
contexts: [ /* paths to contexts */ ],
linkingRules: {
/* key: value */
},
dest: /* destination path */
},
c = new Concoction(options);
c.concoct(function(err) {
/* Done. Check err. */
});ConcoctJS provides logs on four levels: info, error, warn and debug.
Logging is enabled/disabled with the environment variable DEBUG.
To enable logging on all levels set DEBUG=concoct:*.
To enable logging on all levels except debug set DEBUG="concoct:* -concoct:debug".