Selmer
Selmer copied to clipboard
Is it possible to apply a filter to the output of a tag?
I have a custom tr
tag that returns internationalized strings from a dictionary, and I'd like to apply a Selmer filter to its output. Is that possible?
As a simple example: say {% tr core/greeting %}
parses to Hello
or Hola
depending on a user's current language preference, and I'd like to then apply |upper
to the output of that tag.
There isn't a way to do that currently, but you can access the filters in the tag implementation, e.g: (:upper @selmer.filters/filters)
.
@unitof a tag is expected to output a rendered template, while a filter is an operation on a variable. what you could do is create a filter tag, which you could wrap a tag in, which would apply an existing filter to the contents in a tag. this doesn't make sense for a lot of filters.
{% filter uppercase %}
{% tr core/greeting %}
{% endfilter %}
another option is to add a filter as a tag param
{% tr core/greeting filters=uppercase|replace:whatever %}
something like that. and how far you go with parsing would be up to you.
i think a param is probably better than a wrapper tag. i haven't run into needing to apply filters to tags, and i do a lot of weird stuff with templating. i think i would likely approach your tr
tag solution as a filter itself, but that's a person preference.