Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 具有多个结构的golang模板_Json_Templates_Go - Fatal编程技术网

Json 具有多个结构的golang模板

Json 具有多个结构的golang模板,json,templates,go,Json,Templates,Go,我有一个包含JSON字段的结构,如下所示: 详细信息:=&详细信息{ 名称字符串 详细信息json.RawMessage } 模板如下所示: 详细信息=在{{Name}{{CreatedAt}}{{UpdatedAt}} 我的问题是,我们可以为单个模板使用一个或多个结构,还是仅限于一个结构。您可以传递任意多的内容。您没有提供太多的示例,因此我将假设一些事情,但以下是您将如何处理它: // Shorthand - useful! type M map[string]interface func

我有一个包含JSON字段的结构,如下所示:

详细信息:=&详细信息{ 名称字符串 详细信息json.RawMessage }

模板如下所示:

详细信息=在{{Name}{{CreatedAt}}{{UpdatedAt}}


我的问题是,我们可以为单个模板使用一个或多个结构,还是仅限于一个结构。

您可以传递任意多的内容。您没有提供太多的示例,因此我将假设一些事情,但以下是您将如何处理它:

// Shorthand - useful!
type M map[string]interface

func SomeHandler(w http.ResponseWriter, r *http.Request) {
    detail := Detail{}
    // From a DB, or API response, etc.
    populateDetail(&detail)

    user := User{}
    populateUser(&user)

    // Get a session, set headers, etc.

    // Assuming tmpl is already a defined *template.Template
    tmpl.RenderTemplate(w, "index.tmpl", M{
        // We can pass as many things as we like
        "detail": detail,
        "profile": user,
        "status": "", // Just an example
    }
}
。。。以及我们的模板:

<!DOCTYPE html>
<html>
<body>
    // Using "with"
    {{ with .detail }}
        {{ .Name }}
        {{ .CreatedAt }}
        {{ .UpdatedAt }}
    {{ end }}

    // ... or the fully-qualified way
    // User has fields "Name", "Email", "Address". We'll use just two.
    Hi there, {{ .profile.Name }}!
    Logged in as {{ .profile.Email }}
</body>
</html>

希望澄清。

您可以传递任意多的内容。您没有提供太多的示例,因此我将假设一些事情,但以下是您将如何处理它:

// Shorthand - useful!
type M map[string]interface

func SomeHandler(w http.ResponseWriter, r *http.Request) {
    detail := Detail{}
    // From a DB, or API response, etc.
    populateDetail(&detail)

    user := User{}
    populateUser(&user)

    // Get a session, set headers, etc.

    // Assuming tmpl is already a defined *template.Template
    tmpl.RenderTemplate(w, "index.tmpl", M{
        // We can pass as many things as we like
        "detail": detail,
        "profile": user,
        "status": "", // Just an example
    }
}
。。。以及我们的模板:

<!DOCTYPE html>
<html>
<body>
    // Using "with"
    {{ with .detail }}
        {{ .Name }}
        {{ .CreatedAt }}
        {{ .UpdatedAt }}
    {{ end }}

    // ... or the fully-qualified way
    // User has fields "Name", "Email", "Address". We'll use just two.
    Hi there, {{ .profile.Name }}!
    Logged in as {{ .profile.Email }}
</body>
</html>

希望澄清。

Detail是一个结构,它包含一个名称字符串和一堆字节的json.RawMessage。您的模板提到CreatedAt和UpdatedAt。如果您的目标是从json.RawMessage中提取字段并将其放入模板中,那么有很多方法可以做到这一点。你能澄清你的问题吗?是的,我想从json中提取字段并将它们添加到同一个模板中。还有我的问题,有没有一种方法可以为一个模板使用多个结构?好吧,你可以为你的模板只传入一个对象,但是这个对象可以是,例如,你想要的任何类型的结构数组。HTH.你能提供一个有效的playgroud代码片段来说明你想要做什么吗?我最初有一个模板的多个结构,但我将这些结构嵌入到一个结构中。这解决了我的问题详细信息是一个结构,它包含一个名称字符串和一堆字节的json.RawMessage。您的模板提到CreatedAt和UpdatedAt。如果您的目标是从json.RawMessage中提取字段并将其放入模板中,那么有很多方法可以做到这一点。你能澄清你的问题吗?是的,我想从json中提取字段并将它们添加到同一个模板中。还有我的问题,有没有一种方法可以为一个模板使用多个结构?好吧,你可以为你的模板只传入一个对象,但是这个对象可以是,例如,你想要的任何类型的结构数组。你能提供一个有效的playgroud片段来说明你想要做什么吗?我最初有多个模板结构,但我将这些结构嵌入到一个结构中,这解决了我的问题