Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support an optional custom escape #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

fardog
Copy link

@fardog fardog commented Mar 22, 2018

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 added 2 commits March 22, 2018 10:35
**Warning:** This changes the public API, requiring a `nil` to be
passed to template parsing functions in cases where the default
parser is intended to be used.

This adds an optional `TemplateOptions` parameter to template
parsing functions, which allows specification of a newly-added
`Escaper` interface, which defines a string escaping function.

By default this will be an HTMLEscaper, which has idential
behavior to previous versions of this library: it escapes HTML.
However, for usages where you're _not_ rendering HTML (say JSON,
YAML, plain text, etc.) you may specify your own escaping
function to be used by default.
@fardog
Copy link
Author

fardog commented Mar 23, 2018

sorry about that, missed updating some tests; fixed now

@lumasepa
Copy link

lumasepa commented Apr 4, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants