interdependence / tree-sitter-htmldjango

A tree-sitter grammar for the Django template language
MIT License
19 stars 5 forks source link

As a person who wants to translate strings, I would like support for "string | filter" #5

Open jhthorsen opened 4 months ago

jhthorsen commented 4 months ago

Thank you for adding django support to tree-sitter!

I was wondering if it could be possible to add support for the syntax below:

<html>
  {{ "foo" }}
  {{ 'foo'|filter }}
  {{ "foo"|filter:argument }}
</html>

My usecase is to use it for translations:

{{ 'Hello %1!' | i18n }}
interdependence commented 4 months ago

Thanks for the issue! The Django docs are very specific regarding variable names:

Variable names consist of any combination of alphanumeric characters and the underscore ("_") but may not start with an underscore, and may not be a number. The dot (".") also appears in variable sections, although that has a special meaning, as indicated below. Importantly, you cannot have spaces or punctuation characters in variable names.

It never occurred to me that any other characters (i.e. quotes) should be checked for, since I only considered the case of variables names (and optional filters) contained within {{ }} and not string literals like your example.

Can you find any documentation that demonstrates this syntax?

Espencer123 commented 1 month ago

Happened to be reading the Django docs right after seeing this post and came across the example of {{ my_date|date:"Y-m-d" }} in Template Filters,

interdependence commented 1 month ago

That case should work fine as is, since the string is used as an argument to the date filter.