raymond icon indicating copy to clipboard operation
raymond copied to clipboard

support an optional custom escape

Open fardog opened this issue 7 years ago • 2 comments

This adds an option to override the default escaping, allowing you to use handlebars to render things which are not HTML. It both alters the public API—is a breaking change—and adds a feature that might not be the common use case for this library, but I wanted to PR these changes against the canonical repo before creating a fork, just in case you thought they would be useful!

This does the following:

  • Adds a new Escaper interface, which defines an Escape method that can be used to escape unsafe text
  • Modifies the existing Escape into HTMLEscaper which satisfies the Escaper interface
  • Adds a TemplateOptions struct, which defines options for a template, such as its Escaper
  • Adds TemplateOptions as an optional parameter to template parsing functions; when nil, the old behavior is maintained
  • Fixes escape_tests.go which had no asserts, and adds a test for the new feature.

Let me know what you think, or maybe if you like the idea but would prefer it implemented in a different way. It won't hurt my feelings if you close this PR; it's an unnecessary addition for most users, changes the public API, and I don't mind maintaining a fork for my purposes.

fardog avatar Mar 22 '18 17:03 fardog

sorry about that, missed updating some tests; fixed now

fardog avatar Mar 23 '18 22:03 fardog

I would like to see this feature in Raymond. I'm using it for non html templates and starts to be a problem having to use always the triple mustache for interpolation. If this API is not ok for the author I can change it to be ok.

lumasepa avatar Apr 04 '18 11:04 lumasepa