我想在 Golang 模板中顯示某個 WooCommerce 產品自定義屬性的值。type Produkt struct { ... Attributes []struct { ID int `json:"id"` Name string `json:"name"` Position int `json:"position"` Visible bool `json:"visible"` Variation bool `json:"variation"` Options []string `json:"options"` } ...}實際的 json 對象如下所示:{ ... "attributes": [ {}, { "id": 2, "name": "Hersteller", "position": 5, "visible": true, "variation": false, "options": [ "Lana Grossa" ] }, {} ], ... }因此,從這個示例中,我想找到屬性數組的名稱 =“Hersteller”的元素的“選項”數組 (Lana Grossa) 的第一個元素。我試圖調整語法以按索引獲取元素,但無法讓它工作......<input type="text" value="{{ (index (value .Produkt.Attributes.Name eq "Hersteller").Options 0) }}"/><input type="text" value="{{ (index (Name .Produkt.Attributes eq "Hersteller").Options 0) }}"/><input type="text" value="{{ (index (.Produkt.Attributes.Name["Hersteller"]).Options 0) }}"/>非常感謝任何提示
1 回答

楊魅力
TA貢獻1811條經驗 獲得超6個贊
使用模板沒有簡單的方法可以做到這一點。你要先找到你需要的入口,然后看它的內容
{{$name := "" }}
{{ range .Product.Attributes }}
{{if eq .Name "Hersteller"}}
{{$name = (index .Options 0)}}
{{end}}
{{ end }}
<input type="text" value="{{$name}}"/>
- 1 回答
- 0 關注
- 194 瀏覽
添加回答
舉報
0/150
提交
取消