Friday, March 1, 2024
HomeGolanguse base template file for golang html/template? - Getting Assist

use base template file for golang html/template? – Getting Assist


// .go
func Alluser(w http.ResponseWriter, r *http.Request) {

    tpl := template.Should(template.ParseFiles("./tpl/alluser.html", "./tpl/base.html" ))

    rows, err := db.Question("SELECT username, e-mail FROM customers")

    if err != nil {
        http.Error(w, "didn't choose * from customers", 500)
        return
    }
    defer rows.Shut()
    
    for rows.Subsequent() {
        knowledge := new(UserList)
        err := rows.Scan(&knowledge.Username, &knowledge.Electronic mail)

        if err != nil {
            http.Error(w, http.StatusText(500), 500)
            return
        }
        obj := ListPageData{
            Customers: []UserList{
                {knowledge.Username, knowledge.Electronic mail},
            },
        }
        tpl.ExecuteTemplate(w, "base", obj)
    }
    
    if err = rows.Err(); err != nil {
        http.Error(w, http.StatusText(500), 500)
        return
    }
}
// base.html - dad or mum
{{outline "base"}}
<!doctype html>
<html lang="en">
..
<most important function="most important" class="base_main container">
    {{template "content material" .}}
</most important>
</physique>
</html>
{{finish}}
// baby.html
{{outline "content material"}}
<ul class="list-group list-group-flush">
	{{vary .Customers}}
		<li class="list-group-item">
		identify: {{.Username}}, e-mail: {{.Electronic mail}}
	</li>
	{{finish}}
</ul>
{{finish}}

When utilizing the essential template, the enumeration takes place (<!doctype html>) together with the information ( {{vary .Customers}} )

You do not want to outline the “dad or mum” templates. Simply the kid templates. Right here is my “base template” utilizing a number of baby templates

<!DOCTYPE html>
<html lang="en">

{{template "httphead"}}
{{template "physique" "residence"}}
    {{template "icn"}} {{template "nav" "residence"}}
    <most important>
        {{template "hdr_top"}}
        <article>
            {{template "mst_home"}}{{template "det_empty"}}
        </article>
    </most important>
    {{template "httpend"}}
</physique>

</html>

It’s not likely concerning the templates. However I additionally corrected them.


func Alluser(w http.ResponseWriter, r *http.Request) {

    tpl := template.Should(template.ParseFiles("./tpl/alluser.html", "./tpl/base.html" ))

    rows, err := db.Question("SELECT username, e-mail FROM customers")

    if err != nil {
        http.Error(w, "didn't choose * from customers", 500)
        return
    }
    defer rows.Shut()

    var albums []*UserList
    
    for rows.Subsequent() {
        knowledge := new(UserList)
        err := rows.Scan(&knowledge.Username, &knowledge.Electronic mail)

        if err != nil {
            http.Error(w, http.StatusText(500), 500)
            return
        }
        albums = append(albums, knowledge)
        fmt.Println("knowledge", knowledge)
    }
    tpl.ExecuteTemplate(w, "base", albums)
    
    if err = rows.Err(); err != nil {
        http.Error(w, http.StatusText(500), 500)
        return
    }
}

It’s all concerning the for loop, you needed to get out of it.

{{template "./tpl/base.html"}}

{{block "content material" .}}
	
<ul class="list-group list-group-flush">
	{{vary .}}
		<li class="list-group-item">
		identify: {{.Username}}, e-mail: {{.Electronic mail}}
	</li>
	{{finish}}
</ul>
	  
{{finish}}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments