fhd / clostache

{{ mustache }} for Clojure
GNU Lesser General Public License v3.0
318 stars 62 forks source link

add *escape-strategy* #47

Closed edw closed 8 years ago

edw commented 8 years ago

We use templating extensively but it is in the context of creating URLs and therefore HTML escaping doesn't work well for us. This pull request creates a dynamic var, *escape-strategy*, which can be bound to either :html (the default) or :url, and escaping will proceed according to the value.

I can imagine creating some syntax to switch the strategy, as even in HTML sometimes one wants to construct URLs, and having the ability to switch from HTML to URL encoding would be handy. I've therefore added two new pieces of syntax to explicitly choose HTML ({{; name }}) or URL ({{% name }}) encoding.

Here's an example of the dynamic var in use:

(let [template "Hello, {{people}}!"
      data {:people "Felix & Oscar"}]
  (binding [*escape-strategy* :url]
    (println (render template data)))
  (println (render template data)))

The output:

Hello, Felix+%26+Oscar!
Hello, Felix & Oscar!