add a couple of examples for custom filters, including registration. (#1098)

relates to #1089
This commit is contained in:
Tim Diggins 2022-10-05 12:11:42 +01:00 committed by GitHub
parent cb48840191
commit f3a027915c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 38 additions and 0 deletions

View File

@ -1201,6 +1201,44 @@ default. This filter is implemented using Tilt.
You can also define your own filters.
`Haml::Filters::YourCustomFilter#compile` should return
[a Temple expression](https://github.com/judofyr/temple/blob/master/EXPRESSIONS.md).
The simplest example of a filter might be something like:
```ruby
class HelloFilter < Haml::Filters::Base
def compile(_node)
[:static, "hello world"]
end
end
Haml::Filters.registered[:hello] ||= HelloFilter
```
A more complex complex example
```ruby
class BetterFilter < Haml::Filters::Base
def compile(node)
temple = [:multi]
temple << [:static, "hello "]
temple << compile_text(node.value[:text])
temple << [:static, " world"]
temple
end
private
def compile_text(text)
if ::Haml::Util.contains_interpolation?(text)
[:dynamic, ::Haml::Util.unescape_interpolation(text)]
else
[:static, text]
end
end
end
Haml::Filters.registered[:better] ||= BetterFilter
```
See {Haml::Filters} for examples.
## Multiline: `|` {#multiline}