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

Support else ifs in conditional attributes #933

Open
dimmerz92 opened this issue Sep 25, 2024 · 4 comments
Open

Support else ifs in conditional attributes #933

dimmerz92 opened this issue Sep 25, 2024 · 4 comments
Labels
enhancement New feature or request NeedsImplementation Needs implementation parser

Comments

@dimmerz92
Copy link

Describe the bug
Writing a template that contains a simple if, else if, else.

The bug I am experiencing is that the else if is not respected. the else in the else if ends up becoming an attribute in the HTML component and the if statement starts again on the second if with the final else being respected.

Note: I have noticed that the LSP will reformat it, however, I have saved with :noa w to prevent formatting and I still get this issue. So I don't believe this is an LSP issue.

To Reproduce

var DefaultStyle = "some-default-class"

type ButtonProps struct {
    Classes     string
    ButtonStyle string
    VoidStyle   bool
}

templ Button(props ButtonProps) {
    <button
        if props.VoidStyle {
            class={ props.Classes }
        } else if props.ButtonStyle != "" {
            class={ props.ButtonStyle, props.Classes }
        } else {
            class={ DefaultStyle, props.Classes }
        }
    >
        { Test Button }
    </button>
}

Expected behavior
I would expect that the if, else if, and else are all evaluated as necessary.

Screenshots
In use:
Screenshot from 2024-09-25 12-46-39

The relevant part of the component:
Screenshot from 2024-09-25 12-49-48

After the LSP has its way with it:
Screenshot from 2024-09-25 12-52-35

Rendered in the browser:
Screenshot from 2024-09-25 12-51-06

Logs
If the issue is related to IDE support, run through the LSP troubleshooting section at https://templ.guide/commands-and-tools/ide-support/#troubleshooting-1 and include logs from templ

templ info output
Run templ info and include the output.

Desktop (please complete the following information):

(✓) os [ goos=linux goarch=amd64 ]
(✓) go [ location=/usr/local/go/bin/go version=go version go1.22.5 linux/amd64 ]
(✓) gopls [ location=/home/andrew/go/bin/gopls version=golang.org/x/tools/gopls v0.16.2 ]
(✓) templ [ location=/home/andrew/go/bin/templ version=v0.2.778 ]
@dimmerz92
Copy link
Author

There is a more comprehensive example that I just merged into my dev branch here: https://github.com/dimmerz92/htemplx

From a few tests, it looks like it still evaluates correctly?, but it still leaves a vestigial else="" in the html tag.

@joerdav joerdav added bug Something isn't working parser NeedsInvestigation Issue needs some investigation before being fixed labels Sep 25, 2024
@joerdav
Copy link
Collaborator

joerdav commented Sep 25, 2024

I'll take a look at this, I imagine there is a bug in the parser code causing this.

@joerdav
Copy link
Collaborator

joerdav commented Sep 25, 2024

After some more digging I realised this is not a bug, but an unimplemented feature. Currently attributes do not support "else if" only simple if and else clauses.

@joerdav joerdav added enhancement New feature or request NeedsImplementation Needs implementation and removed bug Something isn't working NeedsInvestigation Issue needs some investigation before being fixed labels Sep 25, 2024
@joerdav joerdav changed the title Conditional Attributes: else if Support else ifs in conditional attributes Sep 25, 2024
@dimmerz92
Copy link
Author

dimmerz92 commented Sep 25, 2024

No worries!

Out of curiosity, because it still seems to work as expected, is this just a case of the second following if and else statements potentially still getting evaluated in some cases, but not assigning class a second time?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request NeedsImplementation Needs implementation parser
Projects
None yet
Development

No branches or pull requests

2 participants