1

I'm sending an html/template this model:

type MapModel struct {
Networks      []*NetworkMeta
WaveKey       string

}

The Networks field is defined by another type, NetworkMeta:

type NetworkMeta struct {
NetworkMetaKey string

}

I use the Networks array to produce an html select object:

            <select name="waveKey" id="waveKey">
    {{range .Networks}}
            <option value="{{ .NetworkMetaKey}}" {{if eq .NetworkMetaKey .WaveKey }} selected="selected" {{end}}>
            {{ .NetworkMetaKey }}
            </option>
    {{end}}

Everything here works except the "if eq" equality test. That test returns the error: "WaveKey is not a field of struct type *models.NetworkMeta."

As I understand the html/template eq operator, the comparison tests one value against another (or a group of values), the one separated from the rest by a space. In this case, however, the error seems to indicate that for a field, the compiler ignores the space.

Is there any way to make this equality work? Do I need to write a custom func?

Thanks for any help.

Brent
  • 805
  • 9
  • 20
  • Both fields have to be in the same struct (yes, even if you're iterating over a slice coming from the parent). To my knowledge, there is no other way to do that. – william.taylor.09 Apr 14 '16 at 20:26

1 Answers1

1

dot is iterating through the slice of Networks, so it is of type *NetworkMeta. NetworkMeta doesn't have any fields of WaveKey.

A custom func might be what you want since you are trying to access the values from different scopes.

matt.s
  • 1,698
  • 1
  • 20
  • 29
  • 1
    Ah...this is helpful. Indeed, a quick search turned up the idea that the dollar sign reaches back to the "previous" dot, such that this will work: {{ if eq .NetworkMetaKey $.WaveKey }} ... {{ end }} – Brent Apr 14 '16 at 20:41