Keats / tera2

27 stars 3 forks source link

Filter changes #37

Open Keats opened 3 weeks ago

Keats commented 3 weeks ago

Here is the issue to mention:

Known missing:

Current changes:

uncenter commented 3 weeks ago

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

uncenter commented 3 weeks ago

new filters wanted (no deps only)

wanted changes of behaviour

  1. I know you have some opinions on https://github.com/Keats/tera/issues/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 commented 3 weeks ago

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 commented 3 weeks ago

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 commented 3 weeks ago

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