adhocteam / pushup

Pushup is for making modern, page-oriented web apps in Go
https://pushup.adhoc.dev
MIT License
839 stars 30 forks source link

Partial blocks break for loops #90

Closed lvignoli closed 1 year ago

lvignoli commented 1 year ago

In a fresh project from pushup new (on commit 72be05b), the following index.up cannot be built:

^{ 
    data := []string{"a", "b", "c"}
}

^for a, b := range data {
    <text>^a it is ^b</text>
}

<div>
^partial foo {
    <p>Hey</p>
}
</div>

Output error:

# example/myproject/build
index.up:3: a declared but not used
index.up:3: b declared but not used
build command: building project: building project main executable: exit status 2

Removing the partial fixes the issue.

Observed on a MacBook Pro 2021 (M1 Pro) with macOS 13.1.

lvignoli commented 1 year ago

Somehow the for loop is captured by the partial handler, but truncated. Here is the generated code at line 110 of build/index.up.go

func (up *IndexFooPartial) Respond(w http.ResponseWriter, req *http.Request) error {
    // Begin user Go code and HTML
    {
//line index.up:1
        data := []string{"a", "b", "c"}
//line index.up:2

        for a, b := range data {
        }
//line index.up:11
        io.WriteString(w, "\n\t")
//line index.up:11
//line index.up:11
        io.WriteString(w, "<p>")
//line index.up:11
        io.WriteString(w, "Hey")
        io.WriteString(w, "</p>")
        // End user Go code and HTML
    }
    return nil
}
paulsmith commented 1 year ago

Codegen for partials is a little janky, I expected something like this. Thanks for this report. I think I know the fix but might take a bit of reorganization or additional support code.

paulsmith commented 1 year ago

@lvignoli this should be fixed by #92. You'll have a separate (expected) issue which is that the template won't compile because the data variable declared in the Go code block is not used by the partial. If you use it inside the partial then it should be fine.

^partial foo {
    <p>Hey ^data[0]</p>
}
lvignoli commented 1 year ago

Thank you @paulsmith for the quick fix! 😀