duanpa2143 2018-03-12 07:47
浏览 38
已采纳

如何访问模板中的结构字段

I'd like to compare two variables of string type inside a range loop as follows:

<select name="category" id="category">            
    {{range $c := .cats}}
      <option value="{{$c.Title}}"  {{ if eq $c.Title .category}}active{{end}}>{{$c.Title}}</option>                                       
    {{end}}    
</select>

both $c.Title and category are strings dispatched by the controller.

However, instead on drop down menue being in the rendered template, I get :

can't evaluate field category in type model.category

$c is of struct type category:

type Category struct {
    ID        int       `db:"id"`
    Title     string    `db:"title"`
    Slug      string    `db:"slug"`
    CreatedAt time.Time `db:"created_at"`
}

When I put directly the string value of category instead of .category in the code above, there is no issues.
I'm using gowebapp MVC framework, if it does matter.

How can I fix this?

  • 写回答

1条回答 默认 最新

  • douningqiu4991 2018-03-12 07:51
    关注

    Your .category value you want to compare to is not part of your model, but the template engine will attempt to resolve .category as category being a field or method of your model value.

    This is because the {{range}} action sets the dot . to the successive elements in each iteration.

    To refer to the top-level category, you may use the $ sign like this:

    <select name="category" id="category">            
        {{range $c := .cats}}
          <option value="{{$c.Title}}"  {{ if eq $c.Title $.category}}active{{end}}>{{$c.Title}}</option>                                       
        {{end}}    
    </select>
    

    See this runnable example:

    func main() {
        t := template.Must(template.New("").Parse(src))
        params := map[string]interface{}{
            "category": "Cars",
            "cats": []struct{ Title string }{
                {"Animals"}, {"Cars"}, {"Houses"},
            },
        }
        if err := t.Execute(os.Stdout, params); err != nil {
            panic(err)
        }
    }
    
    const src = `<select name="category" id="category">            
        {{range $c := .cats}}
          <option value="{{$c.Title}}"  {{ if eq $c.Title $.category}}active{{end}}>{{$c.Title}}</option>                                       
        {{end}}    
    </select>`
    

    Output (try it on the Go Playground):

    <select name="category" id="category">            
    
          <option value="Animals"  >Animals</option>
    
          <option value="Cars"  active>Cars</option>
    
          <option value="Houses"  >Houses</option>
    
    </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度