Filters
Filters are a way to add a large block of content to a Haml or Slim template. This is useful for adding content that is not HTML, such as CSS or JavaScript. Filters are added to the template using the colon : character followed by the name of the filter.
| Filter | Haml | Slim | EGO |
|---|---|---|---|
:plain | Yes | No | Use normal HTML/text |
:escaped | Yes | No | Use escaped output <%= value %> |
:preserve | Yes | No | Use normal HTML/text |
:javascript | Yes | Yes | Use <script> |
:css | Yes | Yes | Use <style> |
:go | Yes | Yes | Write Go code directly |
EGO templates do not use filter syntax because they already use normal HTML.
Go
Haml and Slim only. The :go filter is different from the other filters: instead of adding literal content to the rendered output, it adds a block of Go code directly to the generated .goht.go file. Use it for multiline Go statements that don’t fit comfortably on a single - code line.
The code inside the :go filter is included in the generated file as is, so it must be valid Go and can declare variables that later lines in the template use.
CSS
Haml and Slim only. The :css filter is used to add CSS to your templates. The CSS content is added to the template as is.
renders as:
The :css filter also supports interpolation.
JavaScript
Haml and Slim only. The :javascript filter is used to add JavaScript to your templates. The JavaScript content is added to the template as is.
renders as:
The :javascript filter also supports interpolation.
Plain text
Haml only; Slim has no :plain filter. The :plain filter is used to add a block of plain text to your templates. Interpolated values and expressions will be unescaped. Meaning if you include HTML in the values, then you will be getting HTML in your output.
renders as:
Escaped text
Haml only; Slim has no :escaped filter. The :escaped filter is used to add a block of escaped text to your templates.
All plain text, interpolated values, and expressions will be escaped.
renders as:
Preserved text
Haml only; Slim has no :preserve filter. The :preserve filter is used to add a block of preserved text to your templates. Like the :plain filter, interpolated values and expressions will be unescaped. The newlines and leading whitespace will be preserved.
renders as: