Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Built-in filters #18

Open
19 of 47 tasks
andylibrian opened this issue Mar 11, 2017 · 15 comments
Open
19 of 47 tasks

Built-in filters #18

andylibrian opened this issue Mar 11, 2017 · 15 comments

Comments

@andylibrian
Copy link
Contributor

andylibrian commented Mar 11, 2017

Tracking down the built-in filters available in jinja2. So that people can pick these to contribute. Feel free to add / remove items.

ref: https://jinja.palletsprojects.com/en/2.11.x/templates/#builtin-filters

  • abs
  • attr (doesn't really make sense in Rust, unlike in python)
  • batch
  • capitalize
  • center
  • default
  • dictsort
  • escape
  • filesizeformat (https://github.com/djc/askama/pull/161)
  • first
  • float
  • forceescape
  • format
  • groupby
  • indent (https://github.com/djc/askama/pull/131)
  • int
  • join
  • last
  • length (aliases: count)
  • list
  • lower
  • map
  • pprint
  • random
  • reject
  • rejectattr
  • replace
  • reverse
  • round
  • safe
  • select
  • selectattr
  • slice
  • sort
  • string
  • striptags
  • sum
  • title
  • tojson
  • trim
  • truncate
  • upper
  • urlencode
  • urlize
  • wordcount
  • wordwrap
  • xmlattr
@djc
Copy link
Collaborator

djc commented Mar 11, 2017

Thank you, this is really useful!

Note that I'm anything but committed to necessarily copying over all the built-in filters; some of these probably don't make much sense with Rust; others might need an amount of complexity that I'm not sure would make sense to always carry in Askama. But the simple ones probably should all join, and trim is a good example of that.

@mitsuhiko, any thoughts about how you selected whether things could go in the built-ins in Jinja? Any that you regret adding?

@mitsuhiko
Copy link

As of 2017 I'm quite happy with the filters and tests that exist. The ones I feel are unnecessary are upper and lower because realistically you can also convert to string and upper/lower then.

striptags I always thought should not be there because it's quite dirty but it turns out to be reasonably useful in cases where HTML is already passed to a template but you actually want to render a text email.

dictsort and groupby could be better but it's hard to improve now.

@mitsuhiko
Copy link

BTW. I do still plan on porting MarkupSafe to rust. In case there is demand in this (that comes up with filters in Jinja a ton) I can spend some effort on that sooner.

@defyrlt
Copy link
Contributor

defyrlt commented Aug 24, 2017

@djc: what do you think join filter should work with? A few random options:

  • T: AsRef<[String]>
  • A: AsRef<str>, T: AsRef<[A]> (I would go with this one)
  • T: AsRef<[Display]>
  • 'a, T: AsRef<[&'a Display + 'a]>

Upd: added https://github.com/djc/askama/pull/39, let's continue API discussion there

@defyrlt
Copy link
Contributor

defyrlt commented Aug 29, 2017

@djc: could you please clean up the list to leave things you think make sense as filters in askama, so it's easier for contributors to start working on them?

@djc
Copy link
Collaborator

djc commented Aug 30, 2017

@defyrlt good to see your enthusiasm for contributing!

Personally, I mostly prefer that people mention filters that they actually have a use for in their Rust code, rather than us just blindly implementing all the filters for Jinja compatibility.

That said, some other high-level guidance:

  • I think a lot of the type conversions are not necessarily as useful in Rust, where we have a strong type system and the input context is expected to be well-typed already.
  • I prefer filters that can be implemented simply with no extra dependencies required, that is, making functionality from Rust's stdlib easily available to templates.

This was referenced Sep 22, 2018
@zzau13
Copy link
Contributor

zzau13 commented Oct 7, 2018

The following filters are not viable in Rust: attr, default, dictsort, groupby, map, pprint, rejectattr, selectattr, sum and xmlattr.
Some can be done directly in Rust on the template and others don't work in Rust.
You could mark them in the list

@djc
Copy link
Collaborator

djc commented Oct 8, 2018

Could you explain for each of those why you think they aren't viable, please?

@zzau13
Copy link
Contributor

zzau13 commented Oct 8, 2018

It's because they are filters for Python dictionaries and it has no sense in Rust.

@djc
Copy link
Collaborator

djc commented Oct 9, 2018

Why not? Wouldn't the same thing be relevant for HashMap or BTreeMap?

@zzau13
Copy link
Contributor

zzau13 commented Oct 9, 2018

Since templates live within the response function, allocating it inside the heap is a serious performance issue.
You can test the Tera filters that use HashMap with frightening results in performance.
It would be better to build help macros for structures with Option<&'a Value> fields.
Like this , the "self" can be set with which the user should do map! (Field, infield, callback).

@zzau13 zzau13 mentioned this issue Oct 9, 2018
@djc
Copy link
Collaborator

djc commented Oct 10, 2018

It typically won't be the template allocating: the template is just using an allocated HashMap and iterating over it, generating output from the data contained in it. To me that's an important use case for Askama.

@brunobell
Copy link
Contributor

Hi, I can see that abs function is already in askama::filters in source code, but how can I use it in templates?
For now I'm getting this error message: "cannot find function abs in module askama::filters".

And is there any doc about how to write custom filters (especially for numbers)? Thanks!

@brunobell
Copy link
Contributor

And I noticed that the abs filter was removed in docs of v0.9.0 (which was in since v0.7.2). So, what is going on here? thanks @djc

@djc
Copy link
Collaborator

djc commented Jan 16, 2020

I made some changes in 0.9 to make the extra dependencies pulled in by some of the filter implementations optional. In the case of abs, that's the num-traits feature. I tried to make it so that this is enabled by default, but may have failed. Can you investigate?

Defining your own filters is explained in https://docs.rs/askama/0.9.0/askama/#filters, is that explanation not clear enough?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants