Json 将图像传递到变量中的模板
我在我的一个项目中遇到了一个无法解决的问题。我有一系列产品,其中包含标题、说明、价格以及我的商店中有多少件此类产品Json 将图像传递到变量中的模板,json,image,templates,variables,go,Json,Image,Templates,Variables,Go,我在我的一个项目中遇到了一个无法解决的问题。我有一系列产品,其中包含标题、说明、价格以及我的商店中有多少件此类产品 type product struct { Title string Description string Price int Pieces int } 每个产品的图像的URL以字符串形式来自json,按产品分组。我有一个主页,其中包括一些块(页眉、页脚、侧边栏、适用于所有产品的产品卡模板),我将变量产品的内容传递给这些块 <div cla
type product struct {
Title string
Description string
Price int
Pieces int
}
每个产品的图像的URL以字符串形式来自json,按产品分组。我有一个主页,其中包括一些块(页眉、页脚、侧边栏、适用于所有产品的产品卡模板),我将变量产品的内容传递给这些块
<div class="container col-lg-12 main">
{{render "blocks/storefront-header.html"}}
<div class="col-lg-12 padd_class" style="padding-left:10px;padding-right:10px;padding-bottom:10px">
<div class="col-lg-12" style="padding-right:70px;padding-left:70px">
<div class="row">
{{range .myproducts}}
{{template "blocks/product-card.html" .}}
{{end}}
</div>
</div>
</div>
我试着把两个变量传递给同一个模板,一个是产品变量,另一个是图像变量,一个是卡片变量,但没有成功。
有人知道我可以如何做到这一点吗?
谢谢你抽出时间 一个简单的解决方案是将
产品
的声明更改为包含图像URL,例如
type product struct {
Title string
Description string
Price int
Pieces int
ImagesURL []string
}
myproducts := []product {
//initialisation
}
或通过结构组合创建新类型
type productWithImage struct {
product
ImagesURL []string
}
myproducts := []productWithImage {
{product: product {"bracelet", "descr", 10, 7}, ImagesURL: []string{...}},
//initialisation
}
现在,您可以将单个变量传递给模板,并使用{{range.ImagesURL}
访问产品卡.html
中的图像URL
type product struct {
Title string
Description string
Price int
Pieces int
ImagesURL []string
}
myproducts := []product {
//initialisation
}
type productWithImage struct {
product
ImagesURL []string
}
myproducts := []productWithImage {
{product: product {"bracelet", "descr", 10, 7}, ImagesURL: []string{...}},
//initialisation
}