djc / askama

Type-safe, compiled Jinja-like templates for Rust
Apache License 2.0
3.35k stars 215 forks source link

Add support for single quote strings? #1018

Closed GuillaumeGomez closed 4 months ago

GuillaumeGomez commented 4 months ago

jinja spec mentions that strings can be either single or double quotes. It is often useful for syntax highlighting when you have cases like:

<a href="{{ func('bla') }}">

Would you be open for me to implement it?

djc commented 4 months ago

It seems like an anti-feature to me in the context of Askama/Rust -- we would not be able to support char properly.

(Also not sure why double quotes wouldn't work in the example you gave.)

GuillaumeGomez commented 4 months ago

Fair point. What about emitting an error if it's not a char then to prevent having to debug with generated code instead?

Also, as I mentioned, it was only for syntax highlighting. In my IDEs (emacs and sublime text), if I use double quotes strings in double quotes string, they're both a bit lost. :)

djc commented 4 months ago

Fair point. What about emitting an error if it's not a char then to prevent having to debug with generated code instead?

Does it currently error? How? I think making this properly a char would be okay.

Also, as I mentioned, it was only for syntax highlighting. In my IDEs (emacs and sublime text), if I use double quotes strings in double quotes string, they're both a bit lost. :)

Hmm, sounds they're like confused about what syntax they're parsing?

GuillaumeGomez commented 4 months ago

Does it currently error? How? I think making this properly a char would be okay.

jinja considers 'string' the same as "string": both are strings. In the case of askama, it takes 'string' and doesn't make any changes to it, generating invalid rust code since 'string' is not a valid char, which generates this error:

error: character literal may only contain one codepoint

We could detect this error in askama and I think we should. It would make user experience much better.

Hmm, sounds they're like confused about what syntax they're parsing?

Very likely. It's not a big issue, just something I thought could be improved. But having stricter types directly into the templates sounds better to me. We just need to improve how this error is handled in askama.

djc commented 4 months ago

We could detect this error in askama and I think we should. It would make user experience much better.

Sounds good to me.

GuillaumeGomez commented 4 months ago

Gonna send a PR later on then. I'm reaching the end of the port to askama in docs.rs. It's exciting. :)

djc commented 4 months ago

Good to hear!