dsft8327 2017-02-22 09:12
浏览 6

仅在[] struct不为空时继续

I have an []struct that can have no content but also it is possible that it has content:

Anleitung  []struct {
    Name string `json:"blog"`
    Link string `json:"link"`
} `json:"anleitung"`

In my template I try to check if Anleitung contains something, and only then proceed:

{{ ne $jb.Anleitung "" }}
     <div class="btn-group">
        <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              <span class="badge">4</span> Anleitungen <span class="caret"></span>
        </button>
        <ul class="dropdown-menu">
            {{ range $anl := $jb.Anleitung }}
                 <li><a href="{{ $anl.Link }}?ref=jbguide">{{ $anl.Name }}</a></li>
             {{ end }}
         </ul>
      </div>
{{ end }}

compiling the whole thing gives me the following error:

Error rendering index template: template: index.tmpl:131: unexpected {{end}}

Row no 131 is the last row in the template file.

Is there a way to check if the []struct contains any data?

  • 写回答

2条回答 默认 最新

  • doubi1713 2017-02-22 09:22
    关注

    You may simply use the {{with}} action. Quoting from the package doc of text/template:

    {{with pipeline}} T1 {{end}}
        If the value of the pipeline is empty, no output is generated;
        otherwise, dot is set to the value of the pipeline and T1 is
        executed.
    

    Since {{with}} also sets the pipeline, modify your {{range}} to this:

    {{ range $anl := . }}
    

    Using it:

    {{ with $jb.Anleitung }}
         <div class="btn-group">
            <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  <span class="badge">4</span> Anleitungen <span class="caret"></span>
            </button>
            <ul class="dropdown-menu">
                {{ range $anl := . }}
                     <li><a href="{{ $anl.Link }}?ref=jbguide">{{ $anl.Name }}</a></li>
                 {{ end }}
             </ul>
          </div>
    {{ end }}
    

    Going further, you can simplify the {{range}} as we don't need the $anl variable, {{range}} also sets the pipeline:

    {{ range . }}
        <li><a href="{{.Link}}?ref=jbguide">{{.Name}}</a></li>
    {{ end }}
    

    Testing it:

    t := template.Must(template.New("").Parse(templ))
    m := map[string]interface{}{}
    fmt.Println("First, no params:")
    if err := t.Execute(os.Stdout, m); err != nil {
        fmt.Println(err)
    }
    
    fmt.Println("Next, 2 values:")
    m = map[string]interface{}{
        "Anleitung": []struct {
            Name string `json:"blog"`
            Link string `json:"link"`
        }{
            {"Bob", "http://google.com"},
            {"Alice", "http://amazon.com"},
        },
    }
    if err := t.Execute(os.Stdout, m); err != nil {
        fmt.Println(err)
    }
    

    Output (try it on the Go Playground):

    First, no params:
    
    Next, 2 values:
    
    
         <div class="btn-group">
            <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  <span class="badge">4</span> Anleitungen <span class="caret"></span>
            </button>
            <ul class="dropdown-menu">
    
                     <li><a href="http://google.com?ref=jbguide">Bob</a></li>
    
                     <li><a href="http://amazon.com?ref=jbguide">Alice</a></li>
    
             </ul>
          </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False