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

Emulate SafeString somehow #27

Open
thumphries opened this issue Feb 10, 2016 · 4 comments
Open

Emulate SafeString somehow #27

thumphries opened this issue Feb 10, 2016 · 4 comments

Comments

@thumphries
Copy link
Contributor

In Handlebars, SafeStrings are values you can construct that don't get escaped when they appear in a mustache. Apparently we might need them

It's easy enough to add it to Value but will need to rejig BMX.Eval a little bit, as escaping happens after the type has been erased

@damncabbage if you could provide an example of how we'd be using them (generating them from a helper, I presume) that'd help

@damncabbage
Copy link
Contributor

Sure.

A simple example from the handlebars docs:

Handlebars.registerHelper('link', function(text, url) {
  text = Handlebars.Utils.escapeExpression(text);
  url  = Handlebars.Utils.escapeExpression(url);
  var result = '<a href="' + url + '">' + text + '</a>';
  return new Handlebars.SafeString(result);
});

And a probable use for us, based on numberListToJS I'm trying in https://github.com/ambiata/bikeshed/pull/71:

// JSON -> SafeString
// eg. ["Nokia","Samsung","Mozilla"] => '["Nokia", "Samsung", "Mozilla"]'
handlebars.registerHelper('toJSLiteral', function(x) {
  return new handlebars.SafeString(JSON.stringify(x));
});

(This is a bit extreme, but an extension of the numberListToJS, stringListToJS, numberToJS, etc. set of helpers I see coalescing to help provide seed data to the graph (and other) script blocks. The Haskell version of this particular example would be taking advantage of the round-trippability-to/from-JSON nature of the Value type.)

Edit: I can hear @charleso's small scream of horror from here.

@thumphries
Copy link
Contributor Author

Something I don't quite get about either one - why not just call it with an unescaped mustache, like {{{ link "http://blah" }}}? I am probably still missing something...

Value doesn't have a JSON instance anymore. You'd probably use the ToJSON instances of the boxed types instead (Text Integer Bool etc), and use those directly in your helper. I see how you'd want raw JSON output to be unescaped, though.

@damncabbage
Copy link
Contributor

Fair point about {{{ ... }}}s; we can use those in our implementation, and leave the {{ returnsSomeHtml }}-style helpers as an unimplemented todo compatibility item.

@thumphries
Copy link
Contributor Author

@damncabbage don't need this anymore, do we?

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

No branches or pull requests

2 participants