BorisMoore / jsrender

A lightweight, powerful and highly extensible templating engine. In the browser or on Node.js, with or without jQuery.
http://www.jsviews.com
MIT License
2.67k stars 339 forks source link

Debug at individual tag level #328

Closed robbertbrak closed 6 years ago

robbertbrak commented 6 years ago

From the documentation:

If debug mode is set to true – error messages encountered while rendering a template block will replace the rendered content of that block

That's nice, but if a template block is very large, it can be difficult to track down bugs in individual tags.

How about adding a debug mode that acts as if an error handler was added to each individual tag?

Taking the example from the documentation, if this debug mode is used, the template below:

{{for members}}
  <div>{{:name}} - <b>{{:address.street}}</b></div>
{{/for}}

...with this data:

var team = {members: [
 {name:"Jo", address: {street: "1st Ave"}},
 {name:"Bill"}, // Bill does not have an address!!
 {name:"Ava", address: {street: "Main St"}}
]};

...would render as:

Jo - 1st Ave
Bill - {Error: Cannot read property 'street' of undefined}
Ava - Main St
BorisMoore commented 6 years ago

You are right, having error messages rendered at the individual tag level, rather than the template/block level, would make for easier debugging, and would conserve more of the correct about from the template rendering. Historically the debugMode(true) was originally at the template block level, and the tag-level onError overrides were not yet implemented. But now that tag-level code is there it is actually possible to change the default debugMode(true) behavior to be at the tag level. I have made that change in my current working version of the next JsViews/JsRender update. It is technically a breaking change, but I think I prefer to change the default behavior rather than introduce an additional new alternative debug mode.

I have attached the working copies of jsrender.js and jsviews.js. Can you test out whether the change works for you, and let me know if there are any issues?

jsrender.js.txt jsviews.js.txt

Thanks.

robbertbrak commented 6 years ago

It seems to work pretty well, thanks!

I briefly tested various settings of debugMode, such as debugMode(true), debugMode(false), debugMode('foo'), debugMode('') and debugMode(myErrorFunc), with and without onError overrides at the tag level. Both jsrender.js and jsviews.js behave exactly as I would expect.

BorisMoore commented 6 years ago

Excellent. Thanks for testing it!

BorisMoore commented 6 years ago

This feature change has been made in commit 89