a-h / templ

A language for writing HTML user interfaces in Go.
https://templ.guide/
MIT License
8.35k stars 276 forks source link

[Proposal] Improve embedding components #865

Closed Z3NTL3 closed 3 months ago

Z3NTL3 commented 3 months ago

I like templ and it's really nice. However, I think a great addition will be the oppurtunity for our template functions to accept a templ.Component and render the embedded page from within. That's much more convenient for my use-case.

Instead of:

templ TREE(props Page, renderPage string, pageData any) {
    <!DOCTYPE html>
    <html lang="en">
        <head>
            @META(props)
            @IMPORTS()
        </head>
        <body>
            switch renderPage {
                case "home":
                    @pages.Home(pageData.(pages.HomeProps))
            }
        </body>
    </html>
}

Something like that below, is more convenience, compared to the previous snippet:

templ TREE(props Page, page templ.Component) {
    <!DOCTYPE html>
    <html lang="en">
        <head>
            @META(props)
            @IMPORTS()
        </head>
        <body>
            @page()
        </body>
    </html>
}

When I do template generate, i get an error from GOPLS.

Z3NTL3 commented 3 months ago

I've changed it to:

templ TREE(props Page, page func() templ.Component) {
    <!DOCTYPE html>
    <html lang="en">
        <head>
            @META(props)
            @IMPORTS()
        </head>
        <body>
            @page()
        </body>
    </html>
}

or

templ TREE(props Page, page templ.Component) {
    <!DOCTYPE html>
    <html lang="en">
        <head>
            @META(props)
            @IMPORTS()
        </head>
        <body>
            @page
        </body>
    </html>
}

Which after templ generate, the generated file for tree, contained the package import of my embed, however it was not used so had to manually remove that. Doing that resolved this and works.

a-h commented 3 months ago

Hi,

My understanding of what you've written is that you initially thought that templ couldn't accept parameters, so your initial proposal was to make that possible.

However, in your second comment on this issue, you worked out that it could (as per the docs at https://templ.guide/syntax-and-usage/template-composition/

Based on this, I don't think there's an issue to resolve, so I'll close it. Feel free to re-open if I've misunderstood your intention.

Thanks, Adrian.