Helpers
Helpers are global functions that are registered by default when you create the CarrotEngine
. You can
access them in all of your templates, and they provide some useful functionality: from HTML-escaping to formatting
numbers.
HTML
- html.safe(<str>)
- Mark a string as "safe". When
Configuration.Builder#setAutoEscape
is false (which is the default), all output from tags is escaped automatically. If you don't want that to happen
(for example, if the value is from a known safe source), then you can use
html.safe
to mark is as safe for un-escaped output.For example, given the following template:
{{ "Some <b>HTML</b> here" }} {{ html.safe("Some <b>HTML</b> here") }}
You would get:
Some <b>HTML</b> here Some <b>HTML</b> here
- html.escape(<str>)
- Explicitly HTML-escape the given string. This is handy if you want to turn auto-escaping off by default (via
Configuration.Builder#setAutoEscape)
but then want to selectively escape some strings.
Note that calling this over and over will not cause the string to become double-escaped. And calling this when auto-escape is on will have no effect.