Escaping/quoting/filters

520 views
Skip to first unread message

Yang Zhang

unread,
May 12, 2010, 8:00:54 PM5/12/10
to musta...@googlegroups.com
How do users of mustache deal with things like escaping, quoting, and
other things that are typically done with "filters" in most templating
languages (e.g. {{somevar | escape}})? Better yet, any approaches to
auto-escaping for well-known target languages like HTML (a feature in
several frameworks e.g. Django)?
--
Yang Zhang
http://yz.mit.edu/

Juvenn Woo

unread,
May 12, 2010, 11:43:56 PM5/12/10
to musta...@googlegroups.com
Yang,

Specifically, mustache default to escape; when you don't want escape, use `{{{text_with_html}}}`. 

Oops! The mustache manual does not have unescape tag, {{{ }}}?


Best,
--
Juvenn Woo ♫

Chris Wanstrath

unread,
May 13, 2010, 1:29:32 PM5/13/10
to musta...@googlegroups.com
On Wed, May 12, 2010 at 8:43 PM, Juvenn Woo <mac...@gmail.com> wrote:

> Oops! The mustache manual does not have unescape tag, {{{ }}}?

Yes, it's included under "variables" on
http://mustache.github.com/mustache.5.html:

> All variables are HTML escaped by default. If you want to return unescaped HTML, use the triple mustache: {{{name}}}.

> You can also use & to unescape a variable: {{& name}}. This may be useful when changing delimiters (see "Set Delimiter" below).

Chris Wanstrath

unread,
May 13, 2010, 1:32:24 PM5/13/10
to musta...@googlegroups.com
On Wed, May 12, 2010 at 5:00 PM, Yang Zhang <yangha...@gmail.com> wrote:

> How do users of mustache deal with things like escaping, quoting, and
> other things that are typically done with "filters" in most templating
> languages (e.g. {{somevar | escape}})? Better yet, any approaches to
> auto-escaping for well-known target languages like HTML (a feature in
> several frameworks e.g. Django)?

HTML escaping is done by default in Mustache. For filters, it's best
to do any sort of data manipulating in your view.

For example, if you want to format a date somehow:

class MyView < Mustache
def items
@list_of_items
end

def formatted_date
self[:date].in_the_format_i_want
end
end

MyView.template = "
{{# items}}
{{name}} was created at {{formatted_date}}
{{/ items}}
"

Juvenn Woo

unread,
May 13, 2010, 2:40:19 PM5/13/10
to musta...@googlegroups.com

Best,
--
Juvenn Woo ♫




On Fri, May 14, 2010 at 1:29 AM, Chris Wanstrath <ch...@ozmm.org> wrote:
On Wed, May 12, 2010 at 8:43 PM, Juvenn Woo <mac...@gmail.com> wrote:

> Oops! The mustache manual does not have unescape tag, {{{ }}}?

Yes, it's included under "variables" on
http://mustache.github.com/mustache.5.html:

> All variables are HTML escaped by default. If you want to return unescaped HTML, use the triple mustache: {{{name}}}.


Oh, sorry. I've looked over it.
Reply all
Reply to author
Forward
0 new messages