Giter Site home page Giter Site logo

Filters to implement about tera HOT 27 CLOSED

keats avatar keats commented on August 25, 2024
Filters to implement

from tera.

Comments (27)

Keats avatar Keats commented on August 25, 2024 2

I just released 0.2.0 which contains all the filters done so far, thanks @orhanbalci @foophoof @Peternator7 !

from tera.

Peternator7 avatar Peternator7 commented on August 25, 2024 1

Hey I saw this on this-week-in-rust and thought I might give helping out a shot. I just put in a PR for first,last, and join on the array filters. Let me know if you have any feedback, and if they're okay, I might be able to help with a few others!

from tera.

DemiMarie avatar DemiMarie commented on August 25, 2024 1

It would be awesome to have an HTML sanitizing filter (only allowed in content, not in attributes) that allows only a whitelisted safe subset of HTML (for the given context).

from tera.

Keats avatar Keats commented on August 25, 2024 1

@batisteo I've added a default in the 0.11 branch which is basically the default_if_none from Django. I don't really see the usecase of default over default_if_none so if you have some examples, I'll take them!

from tera.

orhanbalci avatar orhanbalci commented on August 25, 2024

@Keats Should title filter respect whitespaces? Documentation does not state anything about whitespaces.

from tera.

Keats avatar Keats commented on August 25, 2024

afaik it's something like split on whitespace, uppercase first char of each and join them with " "

Jinja impl: https://github.com/pallets/jinja/blob/8a49e066af3d88d1edb34aa1680b668c959dfcf7/jinja2/filters.py#L183-L190
Jinja tests: https://github.com/pallets/jinja/blob/f4092239019d9a9130dd84ef6278e58061d6bb93/tests/test_filters.py#L201-L223

(Jinja is a bit smarter since it takes into account thinks like <, { that are not letters)

from tera.

orhanbalci avatar orhanbalci commented on August 25, 2024

@Keats For escape filter can we use an external crate like https://github.com/veddan/rust-htmlescape

from tera.

Keats avatar Keats commented on August 25, 2024

Yeah I've seen of couple on crates.io, need to see which one would be the better fit

from tera.

Keats avatar Keats commented on August 25, 2024

Added escape_js and escape_sql to the list, tempted to rename escape to escape_html once autoescape is in
Note that I'm not super convinced on escape_sql yet

from tera.

clarfonthey avatar clarfonthey commented on August 25, 2024

Was looking at docs and IMO the pluralize filter is kind of confusing and not easily generalisable to multiple languages. I personally think it'd be best to force people to do if statements, because the code is more clear to me to see:

{% if num == 1 %}
    1 message
{% else %}
    {{ num }} messages
{% endif %}

Rather than:

{{ num }} message{{ num | pluralize }}

Especially considering the number of irregular plurals for which this won't work even in English.

Of course, this is just my opinion, but I think that Django having this filter isn't worth adding it for Tera. That's my 2ยข. :)

from tera.

clarfonthey avatar clarfonthey commented on August 25, 2024

Another comment I have on sort is that it should be split into strsort and numsort where strsort sorts lexically and numsort sorts by a version-based sort (i.e. 12.34.56 > 1.2.3) and then a lexcal sort for any non-numeric characters. I feel like this would be a lot clearer than just a sort.

from tera.

Keats avatar Keats commented on August 25, 2024

I agree for the pluralize, it's probably going to get changed when/if I have time for i18n.

For sort, I'm still unsure whether I want to add it at all. The only sort I can see myself doing is a string sort, anything else I'd do it in Rust itself I think

from tera.

gyscos avatar gyscos commented on August 25, 2024

Adding the "precision" parameter for the round filter would be nice.

from tera.

Keats avatar Keats commented on August 25, 2024

@gyscos agreed, opened #99 to track that. PR welcome!

from tera.

Keats avatar Keats commented on August 25, 2024

Added precision, it will be in 0.6

from tera.

clarfonthey avatar clarfonthey commented on August 25, 2024

I'm rather against escape_sql because it encourages client-side query sanitisation which seems bad to me.

from tera.

Keats avatar Keats commented on August 25, 2024

@clarcharr I added the escape_sql after seeing https://github.com/hashedin/jinjasql but I guess we can just replace the variables by ? or whatever pg/mysql expects for prepared statements.

@DemiMarie how would you know the context? I assume you mean for example li only allowed in ul? If there is a HTML sanitization lib in Rust like https://pypi.python.org/pypi/bleach, it should be easy to write your own but I don't think it would be possible for Tera to know the context

from tera.

DemiMarie avatar DemiMarie commented on August 25, 2024

@Keats Such a library does exist (https://crates.io/crates/ammonia). The context would need to be determined by parsing the template.

from tera.

Keats avatar Keats commented on August 25, 2024

But then it requires Tera to parse invalid/incomplete HTML (and CSS, JSON etc from #190) which doesn't seem achievable imo

from tera.

DemiMarie avatar DemiMarie commented on August 25, 2024

@Keats Servo's parsers are browser-grade and are available as libraries.

from tera.

batisteo avatar batisteo commented on August 25, 2024

Trying to access or render a variable that doesn't exist will result in an error.

Iโ€™d like to see the default filter (and maybe default_if_none).

{{ lang | default(value="en") }}

from tera.

Keats avatar Keats commented on August 25, 2024

I believe that should be an easy one to add and would be a "fake" filter, otherwise the expression would fail with lang not being defined if it's not in the context.
If anyone wants to add it, please do it in the 0.11 branch or I'll add it myself next week or so

from tera.

Keats avatar Keats commented on August 25, 2024

Still missing some like truncate but this issue has served its purpose!

from tera.

Hugo-Trentesaux avatar Hugo-Trentesaux commented on August 25, 2024

(still needing the truncate preserve words and preserve tags options)

from tera.

Keats avatar Keats commented on August 25, 2024

I don't know about preserve words, it only works for some languages. I don't think jinja2 has an option to preserve tags?

from tera.

Hugo-Trentesaux avatar Hugo-Trentesaux commented on August 25, 2024

I was thinking about the django filter : https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#truncatechars-html
By the way, striptags misses the html entities like &#123; and so on.
I am planning to have a look on it after I finished migrating my blog from Pelican to Zola.

from tera.

stanislavleonchik avatar stanislavleonchik commented on August 25, 2024

Is anybody know how to use in tera template something like "| floatformat:2" from django?

from tera.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.