Class MustacheComb
Defined in: atropa-mustache-comb.js.
| Constructor Attributes | Constructor Name and Description |
|---|---|
|
MustacheComb(views)
The Base MustacheComb class.
|
| Method Attributes | Method Name and Description |
|---|---|
|
formatExample(text)
Tag handler for the example tag.
|
|
|
includeExample(text)
Function for including example code from files.
|
|
|
includeFile(relPath)
Base Function for including files.
|
|
|
mustacheIncluder(text)
Function for including files.
|
|
|
renderViews(callback)
Renders views from this.views objects array.
|
Class Detail
MustacheComb(views)
The Base MustacheComb class. The templates
and tag handlers shown in their registries will be loaded so, if
you want to use this class directly then you'll need to reference
your own tag handler and template registries.
var MustacheComb, comb;
MustacheComb = require('atropa-mustache-comb').MustacheComb;
comb = new MustacheComb();
// adds handling of "thingy" tags.
// this function must be appended to the prototype in order
// for the addTagHandlerFunction to work properly
// "thingy" tags will be replaced with their original text content
// concatenated with "woop woop!!"
MustacheComb.prototype.formatThingy = function (text) {
return text + ' woop woop!!';
};
comb.addTagHandlerFunction('thingy', 'formatThingy');
// adds "wonderful" template by string
comb.addTemplateByString('wonderful', 'OMFG!! OMFG!! {{greatNews}}\r\n' +
'{{#thingy}}Ain\'t it great news{{/thingy}}\r\n{{>sig}}');
// you could also just add string templates directly to the templates
// property
// comb.templates.wonderful = 'OMFG!! OMFG!! {{greatNews}}\r\n' +
// '{{#thingy}}Ain\'t it great news{{/thingy}}\r\n{{>sig}}';
// if the wonderful template were in a file called wonderful.mustache
// then we could add it with
// comb.addTemplateByFile('wonderful', 'wonderful.mustache', 'utf8');
// a view is just an object.
var sticker = {};
sticker.greatNews = 'I got a free sticker!';
sticker.signature = 'Huzzah!';
// add a view by pushing it onto the views array
comb.views.push(
sticker,
{ 'greatNews' : 'I got a coconut!', 'signature' : 'Jimmy!' },
{ 'greatNews' : 'I got a penny!', 'signature' : 'Jane!' }
);
// to render views you need to define a renderer function
// this renderer function does not have to be appended to the
// MustacheComb prototype but I find it convenient to do so.
// the renderer function will take a view, and render it to the screen,
// to a file, or however you see fit. This renderer function will
// then be given as the argument to renderViews which will pass it
// each view. Or you can do something more complex and confusing.
MustacheComb.prototype.renderer = function renderer(view) {
var out, mustacheTemplateParts;
// the generic page structure used repeatedly.
mustacheTemplateParts = {
"sig" : view.signature
};
out = this.Mustache.to_html(
this.templates.wonderful,
this.mustacheTagHandlers,
mustacheTemplateParts);
console.log(out);
};
comb.renderViews(comb.renderer);
- Parameters:
- {Array} views
- An array of view objects. Defaults to an empty array.
- Requires:
- events
- util
- fs
- path
- mustache
- textTransformers
Method Detail
formatExample(text)
Tag handler for the example tag. Formats the text for html
and prettification.
- Parameters:
- {String} text
- A string of text (example code) to process.
includeExample(text)
Function for including example code from files.
- Parameters:
- {String} text
- The path to the file to include.
- Returns:
- Returns the contents of the file formatted for HTML.
includeFile(relPath)
Base Function for including files. Fetches file contents.
- Parameters:
- {String} relPath
- The path to the file to include.
- Returns:
- Returns the contents of the file or an html link to the unreadable path.
mustacheIncluder(text)
Function for including files. Files may contain mustache tags.
- Parameters:
- {String} text
- The path to the file to include.
- Returns:
- Returns the contents of the file.
renderViews(callback)
Renders views from this.views objects array.
- Parameters:
- callback