在Golang模板中,通过属性值获取结构数组的元素。

huangapple go评论75阅读模式
英文:

Get an element of a struct array by attribute value in a Golang template

问题

我想在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"的元素的"Options"数组的第一个元素(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) }}"/>

非常感谢任何提示。

英文:

I want to display the value of a certain WooCommerce product custom attribute in a Golang template.

type Produkt struct {
   ...
   Attributes []struct {
		ID        int      `json:&quot;id&quot;`
		Name      string   `json:&quot;name&quot;`
		Position  int      `json:&quot;position&quot;`
		Visible   bool     `json:&quot;visible&quot;`
		Variation bool     `json:&quot;variation&quot;`
		Options   []string `json:&quot;options&quot;`
   }
   ...
}

An actual json object looks like this:

{
   ...
   &quot;attributes&quot;: [
   {},
   {
      &quot;id&quot;: 2,
      &quot;name&quot;: &quot;Hersteller&quot;,
      &quot;position&quot;: 5,
      &quot;visible&quot;: true,
      &quot;variation&quot;: false,
      &quot;options&quot;: [
        &quot;Lana Grossa&quot;
      ]
    },
   {}
   ],
   ... 
}

So from this example I want to find the first element of the 'Options' array (Lana Grossa) of the element with the name = "Hersteller" of the attributes array.

I tried to adapt the syntax to get an element by index, but could not get it to work...

&lt;input type=&quot;text&quot; value=&quot;{{ (index (value .Produkt.Attributes.Name eq &quot;Hersteller&quot;).Options 0) }}&quot;/&gt;
&lt;input type=&quot;text&quot; value=&quot;{{ (index (Name .Produkt.Attributes eq &quot;Hersteller&quot;).Options 0) }}&quot;/&gt;
&lt;input type=&quot;text&quot; value=&quot;{{ (index (.Produkt.Attributes.Name[&quot;Hersteller&quot;]).Options 0) }}&quot;/&gt;

Any hint is greatly appreciated

答案1

得分: 2

这是要翻译的内容:

使用模板没有简单的方法来做到这一点。你首先需要找到你需要的条目,然后查看它的内容。

{{$name := "" }}
{{ range .Product.Attributes }}
{{if eq .Name "Hersteller"}}
   {{$name = (index .Options 0)}}
{{end}}
{{ end }}
<input type="text" value="{{$name}}"/>
英文:

There is no simple way to do this with templates. You have to first find the entry you need, and then look at its contents

{{$name := &quot;&quot; }}
{{ range .Product.Attributes }}
{{if eq .Name &quot;Hersteller&quot;}}
   {{$name = (index .Options 0)}}
{{end}}
{{ end }}
&lt;input type=&quot;text&quot; value=&quot;{{$name}}&quot;/&gt;

huangapple
  • 本文由 发表于 2021年12月28日 05:45:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/70500739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定