Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Trailing Commas in Arrays Inside Template Expressions #1086

Closed
Aequitosh opened this issue Aug 23, 2024 · 2 comments · Fixed by #1097
Closed

Allow Trailing Commas in Arrays Inside Template Expressions #1086

Aequitosh opened this issue Aug 23, 2024 · 2 comments · Fixed by #1097

Comments

@Aequitosh
Copy link

Basically what the title says.

To give an example of what I mean, this here compiles just fine:

<nav class="flex justify-between items-center max-w-7xl gap-x-12">
    {% for (route, name) in [
        ("/about", "About"),
        ("/contact", "Contact"),
        ("/events", "Events"),
        ("/history", "History")
    ] %}
    <div class="gap-x-6 leading-6 hover:bg-gray-200">
        <a href="{{ route }}"><span>{{ name }}</span></a>
    </div>
    {% endfor %}
</nav>

But this here doesn't:

<nav class="flex justify-between items-center max-w-7xl gap-x-12">
    {% for (route, name) in [
        ("/about", "About"),
        ("/contact", "Contact"),
        ("/events", "Events"),
        ("/history", "History"),
    ] %}
    <div class="gap-x-6 leading-6 hover:bg-gray-200">
        <a href="{{ route }}"><span>{{ name }}</span></a>
    </div>
    {% endfor %}
</nav>

Note the little comma after the ("/history", "History") tuple in the second example.

I'm not sure if this is trivial to implement or not, but I thought I might bring it to y'alls' attention.

It wasn't that of a big deal in the end though, so I wouldn't call this a "major issue" or whatever, not by a long shot. Though, since Rust allows such trailing commas, I think askama should as well.

For reference, this is the error I got:

error: problems parsing template source at row 30, column 31 near:
       ",\n    ] %}\n    <div class=\"gap-x-6 leadi"...
  --> src/routes/mod.rs:96:10
   |
96 | #[derive(Template)]
   |          ^^^^^^^^
   |
   = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

Running with RUSTFLAGS="-Zmacro-backtrace" cargo +nightly build gives the following:

error: problems parsing template source at row 30, column 31 near:
       ",\n    ] %}\n    <div class=\"gap-x-6 leadi"...
  --> src/routes/mod.rs:96:10
   |
96 | #[derive(Template)]
   |          ^^^^^^^^ in this derive macro expansion
   |
  ::: /home/max/.cargo/registry/src/index.crates.io-6f17d22bba15001f/askama_derive-0.12.5/src/lib.rs:22:1
   |
22 | pub fn derive_template(input: TokenStream) -> TokenStream {
   | --------------------------------------------------------- in this expansion of `#[derive(Template)]`

Neither error was too helpful, but it does point to line 30, column 31, which in my code is precisely the closing parenthesis before the trailing comma; careful reading helps at least.

@djc
Copy link
Collaborator

djc commented Aug 23, 2024

Should be easy enough to implement, do you want to try submitting a PR? You'll want to start here:

https://github.com/djc/askama/blob/main/askama_parser/src/expr.rs#L263

@Aequitosh
Copy link
Author

Oh yeah sure! I'll see to it once I have the time. Doesn't look too complicated at first glance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants