doumou3883 2015-03-24 07:36
浏览 102

如果value为0,则输入值将为空,否则为value

Problem: I have created a simple form, where there's an input field "num". After submission I want to show the value of num in the same input field, in other words want to retain the input in that field. If the value was set to 0 then I want to ignore that.

I can do it in several languages but I'm not sure about how to do it in Golang. My current template file has,

<input type="text" placeholder="foo" name="bar" value="{{if gt .N 0 }} {{.N}} {{end}} "/> 

Server file contains:

data := &listOfReport {
        R: r,
        I: i,
        N: n
}
listTmpl := template.Must(template.New("list_tmpl").Parse(string(report.Template["xxx.tmpl"])))
if err := listTmpl.Execute(w, data); err != nil {
    http.Error(w, fmt.Sprintf("Error rendering template %v", err), 500)
}

Another thought is to make N a string so make it '' or value in the server file. But that actually spoils the variable's name/purpose.

Is there any better way to do it? Is ther any better way to access GET parameters directly from template? Please note that the value of N is originally got from a GET variable.

*This code is not tested

  • 写回答

2条回答 默认 最新

  • dongtun2459 2015-03-24 08:24
    关注

    There is no standard/builtin way to get any request parameters from within a template, you'll have to put it into your data. (You could write a function which does this for you, but that will result in an ugly hack.)

    I don't see what's wrong with your solution.

    评论

报告相同问题?

悬赏问题

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