Golang: Pass Nested Struct to Htmltemplate

Asked

Viewed 42 times

1

Good afternoon to all!

I need a help, I have two structs that are in the controller package as below:

type ListValidation struct{
   Id int
   Nameplan string
   Escop string
   Data string
}

type ListTests struct{
   Id int
   TestName string
   TestsSteps string
   DataNow string
}

The values of these two structs are filled and then passed to another struct that is in the package "Routes":

validation, tests := controller.ExecutValidationPage(id)

type DadosRecebidos struct{
   DadosStructValidation controller.ListValidation
   DadosStructTests controller.ListTests
}

dadosStruct := DadosRecebidos{}

dadosStruct.DadosStructValidation = validation
dadosStruct.DadosStructTests = tests

tmplt.ExecuteTemplate(w, "execvalidation", dadosStruct)

As the above code I am passing the data to my html template, however I do not know how to display the data in the html tags, when I use a nonnested struct I just need to declare {{. Nameplan}} between the tags that the value is passed, but with this kind of nested struct I don’t know how. They could give me a help?.

1 answer

1


There are two ways to solve this...

By 'encoding/json' and 'interface{}'

type ListValidation struct {
    Id       int
    Nameplan string
    Escop    string
    Data     string
}

lv := ListValidation{
    Id:       10,
    Nameplan: "NamePlan sample",
    Escop:    "Escop sample",
    Data:     "Data sample",
}

type ListTests struct {
    Id         int
    TestName   string
    TestsSteps string
    DataNow    string
}

lt := ListTests{
    Id:         20,
    TestName:   "TestName sample",
    TestsSteps: "TestsStepes sample",
    DataNow:    "DataNow sample",
}

type DadosRecebidos struct {
    DadosStructValidation ListValidation
    DadosStructTests      ListTests
}

dr := DadosRecebidos{
    lv,
    lt,
}

var data interface{}
dataBytes, _ := json.Marshal(dr)
json.Unmarshal(dataBytes, &data)

tmpl := template.Must(template.ParseFiles("./views/test.html"))
tmpl.Execute(w, data)

Or by library "github.com/Fatih/structs"

type ListValidation struct {
    Id       int
    Nameplan string
    Escop    string
    Data     string
}

lv := ListValidation{
    Id:       10,
    Nameplan: "NamePlan sample",
    Escop:    "Escop sample",
    Data:     "Data sample",
}

type ListTests struct {
    Id         int
    TestName   string
    TestsSteps string
    DataNow    string
}

lt := ListTests{
    Id:         20,
    TestName:   "TestName sample",
    TestsSteps: "TestsStepes sample",
    DataNow:    "DataNow sample",
}

type DadosRecebidos struct {
    DadosStructValidation ListValidation
    DadosStructTests      ListTests
}

dr := DadosRecebidos{
    lv,
    lt,
}

mp := structs.Map(dr)

tmpl := template.Must(template.ParseFiles("./views/test.html"))
tmpl.Execute(w, mp)

In Html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Request Test</h1>
    {{.DadosStructValidation.Nameplan}}<br>
    {{.DadosStructTests.TestName}}

</body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.