Skip to content

Commit f3cadb9

Browse files
committed
Allow uncompiled templates
1 parent 2aaae31 commit f3cadb9

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ The HTML output accepts a Javascript object with configuration. Possible options
9898
- `synchronisedScroll`: scroll both panes in side-by-side mode: `true` or `false`, default is `false`
9999
- `matchWordsThreshold`: similarity threshold for word matching, default is 0.25
100100
- `matchingMaxComparisons`: perform at most this much comparisons for line matching a block of changes, default is `2500`
101+
- `templates`: object with previously compiled templates to replace parts of the html
102+
- `rawTemplates`: object with raw not compiled templates to replace parts of the html
103+
> For more information regarding the possible templates look into [src/templates](https://github.com/rtfpessoa/diff2html/tree/master/src/templates)
101104
102105
## Diff2HtmlUI Helper
103106

src/hoganjs-utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
function HoganJsUtils(configuration) {
1818
this.config = configuration || {};
1919
extraTemplates = this.config.templates || {};
20+
21+
var rawTemplates = this.config.rawTemplates || {};
22+
for (var templateName in rawTemplates) {
23+
if (rawTemplates.hasOwnProperty(templateName)) {
24+
if (!extraTemplates[templateName]) extraTemplates[templateName] = this.compile(rawTemplates[templateName]);
25+
}
26+
}
2027
}
2128

2229
HoganJsUtils.prototype.render = function(namespace, view, params) {

test/hogan-cache-tests.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,35 @@ describe('HoganJsUtils', function() {
3636
assert.equal(null, result);
3737
});
3838

39-
it('should allow templates to be overridden', function() {
39+
it('should allow templates to be overridden with compiled templates', function() {
4040
var emptyDiffTemplate = HoganJsUtils.compile('<p>{{myName}}</p>');
4141

4242
var config = {templates: {'generic-empty-diff': emptyDiffTemplate}};
4343
var hoganUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)(config);
4444
var result = hoganUtils.render('generic', 'empty-diff', {myName: 'Rodrigo Fernandes'});
4545
assert.equal('<p>Rodrigo Fernandes</p>', result);
4646
});
47+
48+
it('should allow templates to be overridden with uncompiled templates', function() {
49+
var emptyDiffTemplate = '<p>{{myName}}</p>';
50+
51+
var config = {rawTemplates: {'generic-empty-diff': emptyDiffTemplate}};
52+
var hoganUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)(config);
53+
var result = hoganUtils.render('generic', 'empty-diff', {myName: 'Rodrigo Fernandes'});
54+
assert.equal('<p>Rodrigo Fernandes</p>', result);
55+
});
56+
57+
it('should allow templates to be overridden giving priority to compiled templates', function() {
58+
var emptyDiffTemplate = HoganJsUtils.compile('<p>{{myName}}</p>');
59+
var emptyDiffTemplateUncompiled = '<p>Not used!</p>';
60+
61+
var config = {
62+
templates: {'generic-empty-diff': emptyDiffTemplate},
63+
rawTemplates: {'generic-empty-diff': emptyDiffTemplateUncompiled}
64+
};
65+
var hoganUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)(config);
66+
var result = hoganUtils.render('generic', 'empty-diff', {myName: 'Rodrigo Fernandes'});
67+
assert.equal('<p>Rodrigo Fernandes</p>', result);
68+
});
4769
});
4870
});

0 commit comments

Comments
 (0)