youtube / spitfire

A high-performance Python template language
BSD 3-Clause "New" or "Revised" License
403 stars 59 forks source link

Question: any way to use custom filters? #70

Closed mirceaulinic closed 7 years ago

mirceaulinic commented 7 years ago

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

awbraunstein commented 7 years ago

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
mirceaulinic commented 7 years ago

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?

awbraunstein commented 7 years ago

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.

mirceaulinic commented 7 years ago

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.

awbraunstein commented 7 years ago

Yes. I think a wiki is the route we will go for documentation. Thanks for the suggestion.

awbraunstein commented 7 years ago

If you're interested, you can follow the documentation issue here: https://github.com/youtube/spitfire/issues/71