Comments (27)
I just released 0.2.0 which contains all the filters done so far, thanks @orhanbalci @foophoof @Peternator7 !
from tera.
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.
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.
@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.
@Keats Should title filter respect whitespaces? Documentation does not state anything about whitespaces.
from tera.
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.
@Keats For escape filter can we use an external crate like https://github.com/veddan/rust-htmlescape
from tera.
Yeah I've seen of couple on crates.io, need to see which one would be the better fit
from tera.
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.
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.
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.
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.
Adding the "precision" parameter for the round
filter would be nice.
from tera.
@gyscos agreed, opened #99 to track that. PR welcome!
from tera.
Added precision
, it will be in 0.6
from tera.
I'm rather against escape_sql
because it encourages client-side query sanitisation which seems bad to me.
from tera.
@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.
@Keats Such a library does exist (https://crates.io/crates/ammonia). The context would need to be determined by parsing the template.
from tera.
But then it requires Tera to parse invalid/incomplete HTML (and CSS, JSON etc from #190) which doesn't seem achievable imo
from tera.
@Keats Servo's parsers are browser-grade and are available as libraries.
from tera.
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.
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.
Still missing some like truncate but this issue has served its purpose!
from tera.
(still needing the truncate preserve words and preserve tags options)
from tera.
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.
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 {
and so on.
I am planning to have a look on it after I finished migrating my blog from Pelican to Zola.
from tera.
Is anybody know how to use in tera template something like "| floatformat:2" from django?
from tera.
Related Issues (20)
- [FEATURE REQUEST] Support comparison operators for strings HOT 3
- Is there a function similar to minijinja's render_block? HOT 3
- Escaping is not applied at the expected level HOT 5
- Ability to build an object or hashmap? HOT 8
- error while importing HOT 2
- error while importing HOT 1
- Filter sections but set the result in a variable HOT 1
- Excessive memory usage of Tera HOT 3
- linebreaksbr is not safe - add `html_encode` filter? HOT 1
- `default` filter: make the `value` optionnal and add `d` alias HOT 1
- Use of `(` and `)` to specify order of operations. HOT 2
- Weird behavior with duplicate variable names HOT 1
- Removing space between HTML tags HOT 1
- Macro names can have underscores but not hyphens HOT 1
- Macro from another macro HOT 1
- More complete Display implementation for Error HOT 5
- Missing a few git tags? HOT 1
- [Question] Help understanding the `indent` built-in
- How to use in the template has . variables
- Round a number with precision not work as expected! HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tera.