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

Filter changes #37

Open
Keats opened this issue Jun 13, 2024 · 6 comments
Open

Filter changes #37

Keats opened this issue Jun 13, 2024 · 6 comments

Comments

@Keats
Copy link
Owner

Keats commented Jun 13, 2024

Here is the issue to mention:

  • missing filters from v1
  • wanted changes of behaviour
  • new filters wanted (no deps only)

Known missing:

  • sort: do people use it? It's a lot of code

Current changes:

  • trim filters have been merged in trim/trim_start/trim_end with an optional pat argument for start/end rather than separate filters
  • int and float filter do not have a default anymore
  • round filter does not take a common method anymore, it's the default and should not be filled if needed
  • indent filter now takes a width param rather than prefix
  • map, group_by and filter filter will error if the attribute ends up being undefined on one of the value
  • as_str has been renamed to str
@uncenter
Copy link

I could be wrong but it looks like you also changed as_str to str?

@uncenter
Copy link

new filters wanted (no deps only)

  • pad (see Add pad filter tera#921)
  • repeat(n) - repeat a string n times, result could either be an array or a joined string, though an array would probably be better for broader usecases

wanted changes of behaviour

  1. I know you have some opinions on Please add regex-aware replace filter. tera#421 though I think it should be revisited, at least concerning basic regex - no need for complicated capture groups and such.

  2. It would be nice to be able to use positional args, though this isn't filters-specific as it also applies to macros. Maybe through some prefix before the argument name in the declaration, e.g. {% macro say_hello(*name) %} where the asterisk changes it from a kwarg to a positional arg?

  3. Would it ever be possible to use https://keats.github.io/tera/docs/#map to apply a macro/filter to all elements of an array? This could be implemented without breaking existing behavior since a new kwarg could be used, e.g. | map(func=self::say_hello).

@Keats
Copy link
Owner Author

Keats commented Jun 13, 2024

For repeat I would follow what Python does, eg:

>>> " " * 10
'          '
>>> [0] * 10
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> 

so the output depends on the input.

For pad, I think a format filter would be better so it can be used for padding but also for displaying floats with 2 decimals for example.


  1. I'm not going to work on anything requiring dependencies yet but that could be added later in a tera-contrib crate
  2. I want to keep kwargs only. It's slightly less nice when there's a single required argument but otherwise it's much more readable imo.
  3. If you look at the code, there is a TODO to do exactly that. The main issue from my pov is how to pass arguments to that filter. I didn't consider macro but that could be nice as well.

@uncenter
Copy link

For repeat I would follow what Python does, eg:

>>> " " * 10
'          '
>>> [0] * 10
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> 

so the output depends on the input.

Makes sense to me... are you saying you are okay adding a filter like this?

For pad, I think a format filter would be better so it can be used for padding but also for displaying floats with 2 decimals for example.

Hmm how would displaying a float with 2 decimals work? I'm thinking of pad being used like {{ "def" | pad(len=6, fill="_", before=true) }} -> "___def".

  1. I want to keep kwargs only. It's slightly less nice when there's a single required argument but otherwise it's much more readable imo.

I agree but even that case where there is only a single required argument is pretty common I'd think. Besides, most programming languages default to positional arguments and the only language I can think of off the top of my head that does kwargs is Python, which also supports positional args.

@Keats
Copy link
Owner Author

Keats commented Jun 14, 2024

are you saying you are okay adding a filter like this?

Yes. Either that or overload * to do that automatically in the template without the repeat filter, one or the other.

For format, I'm thinking to basically use the Rust format: https://saghm.github.io/five-rust-things/#padding-format-operator

@azemlya
Copy link

azemlya commented Oct 14, 2024

The sort filter is very necessary. Very much so. I want to use multi-sorting, but at least like in tera v1.

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

3 participants