diff --git a/README.md b/README.md index 132c8a2..46909f2 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,9 @@ The HTML output accepts a Javascript object with configuration. Possible options - `synchronisedScroll`: scroll both panes in side-by-side mode: `true` or `false`, default is `false` - `matchWordsThreshold`: similarity threshold for word matching, default is 0.25 - `matchingMaxComparisons`: perform at most this much comparisons for line matching a block of changes, default is `2500` + - `templates`: object with previously compiled templates to replace parts of the html + - `rawTemplates`: object with raw not compiled templates to replace parts of the html + > For more information regarding the possible templates look into [src/templates](https://github.com/rtfpessoa/diff2html/tree/master/src/templates) ## Diff2HtmlUI Helper diff --git a/src/hoganjs-utils.js b/src/hoganjs-utils.js index 0dda08d..b2e9c27 100644 --- a/src/hoganjs-utils.js +++ b/src/hoganjs-utils.js @@ -17,6 +17,13 @@ function HoganJsUtils(configuration) { this.config = configuration || {}; extraTemplates = this.config.templates || {}; + + var rawTemplates = this.config.rawTemplates || {}; + for (var templateName in rawTemplates) { + if (rawTemplates.hasOwnProperty(templateName)) { + if (!extraTemplates[templateName]) extraTemplates[templateName] = this.compile(rawTemplates[templateName]); + } + } } HoganJsUtils.prototype.render = function(namespace, view, params) { diff --git a/test/hogan-cache-tests.js b/test/hogan-cache-tests.js index 3bb754a..a34839c 100644 --- a/test/hogan-cache-tests.js +++ b/test/hogan-cache-tests.js @@ -36,7 +36,7 @@ describe('HoganJsUtils', function() { assert.equal(null, result); }); - it('should allow templates to be overridden', function() { + it('should allow templates to be overridden with compiled templates', function() { var emptyDiffTemplate = HoganJsUtils.compile('

{{myName}}

'); var config = {templates: {'generic-empty-diff': emptyDiffTemplate}}; @@ -44,5 +44,27 @@ describe('HoganJsUtils', function() { var result = hoganUtils.render('generic', 'empty-diff', {myName: 'Rodrigo Fernandes'}); assert.equal('

Rodrigo Fernandes

', result); }); + + it('should allow templates to be overridden with uncompiled templates', function() { + var emptyDiffTemplate = '

{{myName}}

'; + + var config = {rawTemplates: {'generic-empty-diff': emptyDiffTemplate}}; + var hoganUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)(config); + var result = hoganUtils.render('generic', 'empty-diff', {myName: 'Rodrigo Fernandes'}); + assert.equal('

Rodrigo Fernandes

', result); + }); + + it('should allow templates to be overridden giving priority to compiled templates', function() { + var emptyDiffTemplate = HoganJsUtils.compile('

{{myName}}

'); + var emptyDiffTemplateUncompiled = '

Not used!

'; + + var config = { + templates: {'generic-empty-diff': emptyDiffTemplate}, + rawTemplates: {'generic-empty-diff': emptyDiffTemplateUncompiled} + }; + var hoganUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)(config); + var result = hoganUtils.render('generic', 'empty-diff', {myName: 'Rodrigo Fernandes'}); + assert.equal('

Rodrigo Fernandes

', result); + }); }); });