1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-25 01:40:10 +00:00

Improve Underscore.js rendering support.

This commit is contained in:
hacksalot 2016-01-22 10:36:26 -05:00
parent 4fe74057f9
commit 915f35b1e6
2 changed files with 62 additions and 12 deletions

View File

@ -0,0 +1,34 @@
/**
Template helper definitions for Underscore.
@license MIT. Copyright (c) 2016 hacksalot (https://github.com/hacksalot)
@module handlebars-helpers.js
*/
(function() {
var HANDLEBARS = require('handlebars')
, _ = require('underscore')
, helpers = require('./generic-helpers');
/**
Register useful Underscore helpers.
@method registerHelpers
*/
module.exports = function( theme, opts, cssInfo, ctx, eng ) {
helpers.theme = theme;
helpers.opts = opts;
helpers.cssInfo = cssInfo;
helpers.engine = eng;
ctx.h = helpers;
_.each( helpers, function( hVal, hKey ) {
if( _.isFunction( hVal )) {
_.bind( hVal, ctx );
}
}, this);
};
}());

View File

@ -8,7 +8,9 @@ Definition of the UnderscoreGenerator class.
var _ = require('underscore'); var _ = require('underscore')
, registerHelpers = require('../helpers/underscore-helpers')
, HMSTATUS = require('../core/status-codes');
/** /**
@ -17,6 +19,23 @@ Definition of the UnderscoreGenerator class.
*/ */
var UnderscoreGenerator = module.exports = { var UnderscoreGenerator = module.exports = {
generateSimple: function( data, tpl ) {
try {
// Compile and run the Handlebars template.
var template = _.template( tpl );
return template( data );
}
catch( ex ) {
throw {
fluenterror: template ?
HMSTATUS.invokeTemplate : HMSTATUS.compileTemplate,
inner: ex
};
}
},
generate: function( json, jst, format, cssInfo, opts, theme ) { generate: function( json, jst, format, cssInfo, opts, theme ) {
// Tweak underscore's default template delimeters // Tweak underscore's default template delimeters
@ -31,23 +50,20 @@ Definition of the UnderscoreGenerator class.
// Strip {# comments #} // Strip {# comments #}
jst = jst.replace( delims.comment, ''); jst = jst.replace( delims.comment, '');
var helpers = require('../helpers/generic-helpers'); var ctx = {
helpers.opts = opts;
helpers.cssInfo = cssInfo;
// Compile and run the template. TODO: avoid unnecessary recompiles.
var compiled = _.template(jst);
var ret = compiled({
r: format === 'html' || format === 'pdf' || format === 'png' ? json.markdownify() : json, r: format === 'html' || format === 'pdf' || format === 'png' ? json.markdownify() : json,
filt: opts.filters, filt: opts.filters,
XML: require('xml-escape'), XML: require('xml-escape'),
RAW: json, RAW: json,
cssInfo: cssInfo, cssInfo: cssInfo,
//engine: this,
headFragment: opts.headFragment || '', headFragment: opts.headFragment || '',
opts: opts, opts: opts
h: helpers };
});
return ret; registerHelpers( theme, opts, cssInfo, ctx, this );
return this.generateSimple( ctx, jst );
} }
}; };