Question: any way to use custom filters?
Closed this issue · 6 comments
Hello,
Thank you for opensourcing this project!
I'm very new to this library and the first question I have in mind (you may find it trivial): is there any flexible way to extend the list of existing filters?
More specifically, I am thinking about a way to inject external logic. If comfortable with the context, something similar to the Jinja filters.
Is this currently possible or on the roadmap?
Thanks,
Mircea
I believe this is already supported.
If you want to filter something using a custom filter, you can use it like so:
${some_variable|filter=my_custom_filter}
Where my_custom_filter
is some function that is currently in scope. For example, the simplest filter is str, which should essentially be a passthrough.
Your template may look something like this:
#from my.awesome.package import my_awesome_filter
#def main
#set $foo = "my awesome value"
${foo|filter=my_awesome_filter}
#end def
Hi @awbraunstein - thanks for clarifying this!
Please excuse my ignorance, but I didn't find the documentation. Do you have any wiki page or so, having the answer to questions as I had above?
Documentation isn't great, but you can see this syntax referenced here: https://github.com/youtube/spitfire/blob/master/doc/SpitfireVsCheetah.md#placeholders-and-filters-should-be-easier-to-use-in-the-end
Additionally, there are tests that use this syntax too. https://github.com/youtube/spitfire/blob/master/tests/filter_placeholder_2.tmpl#L10
I think that better documentation is something that this project needs. I'll open an issue to track this.
Cheers @awbraunstein - may I suggest adding a dedicated wiki? It's a pretty good Github feature to manage docs.
I will also contribute later when I'll have more experience with spitfire.
Yes. I think a wiki is the route we will go for documentation. Thanks for the suggestion.
If you're interested, you can follow the documentation issue here: #71