diff2html icon indicating copy to clipboard operation
diff2html copied to clipboard

How to customize templates

Open J2BB opened this issue 9 years ago • 7 comments

Hi,

Does somebody know how to customize templates ?

The doc says : https://github.com/rtfpessoa/diff2html#configuration

For example, I would like to customize the "generic-wrapper". (See : https://github.com/rtfpessoa/diff2html/blob/master/src/templates/generic-wrapper.mustache)

But I don't see how to that in options. I suppose I need to replace the "global.browserTemplates["generic-wrapper"]" with something like that : diff2htmlUi.draw( '#diff, { templates: () => { global.browserTemplates["generic-wrapper"] = "<div class=\"d2h-wrapper\"></div>"; } } );

But my IDE say : the property "browserTemplates" does'nt exist on type "Global".

Thanks

J2BB avatar Mar 17 '17 10:03 J2BB

Hi @J2BB for exact usage details check https://github.com/rtfpessoa/diff2html/blob/c2c253d3e3f8b8b267f551e659f72b44ca2ac927/test/hogan-cache-tests.js#L39

I have both types used there. The only difference is that you are passing the config to Diff2html and not HoganJsUtils since it is a hidden component.

Let me know if you have any more questions if everything is fine close this please

rtfpessoa avatar Mar 17 '17 11:03 rtfpessoa

Hi @rtfpessoa ,

Thank you for this fast answer, it works perfectly !

Solution : var HoganJsUtils = new (require('../../../../node_modules/diff2html/src/hoganjs-utils.js').HoganJsUtils)(); diff2htmlUi.draw( '#diff', { templates: { 'generic-wrapper': HoganJsUtils.compile('<div class="d2h-wrapper"></div>') } } );

Thanks !

J2BB avatar Mar 17 '17 14:03 J2BB

Ok, now I understand your problem.

That is definitely a recommended way, you should use the rawTemplates. The idea is that I will compile the rawTemplates and templates is only if you want to depend on hogan and compile them yourself.

rtfpessoa avatar Mar 17 '17 14:03 rtfpessoa

Ok, so i will prefer this way :

{ rawTemplates: { 'generic-wrapper': HoganJsUtils.compile('<div class="d2h-wrapper"></div>') } }

?

J2BB avatar Mar 17 '17 14:03 J2BB

No, you just need the HTML:

{ rawTemplates: { 'generic-wrapper': '<div class="d2h-wrapper"></div>' } }

rtfpessoa avatar Mar 17 '17 14:03 rtfpessoa

Oh ok ;)

And another question : what is the best way to customize the content of the "d2h-file-header" block in a side-by-side diff ?

See : https://github.com/rtfpessoa/diff2html/blob/master/src/templates/line-by-line-file-diff.mustache

J2BB avatar Mar 17 '17 14:03 J2BB

Currently I guess you would have to completely replace the side-by-side-file-diff template. But I would also be open for a PR separating it into it's own template.

rtfpessoa avatar Mar 18 '17 00:03 rtfpessoa