duanji9481 2014-07-07 17:53
浏览 156

html模板中的Golang打印数组值

I dont know how to print values out of an array into a html template This is my struct

type Lampen struct {
Values [10]string
}

and my code for prasing the template is the following

title := "moodlights"
p := &lampen.Lampen{}
err := p.LoadLampValues(title)
if err != nil {
    log.Printf("Error loading Config File")
    for i := range p.Values {
        p.Values[i] = "0"

    }
}
t, _ := template.ParseFiles("template.html")
t.Execute(w, p)

What it does is, that it loads the Values from a JSON File. But now I dont know how to print the Values into the html file. What works is the following:

<div>Lampe0: <input type="text" name="Lampe0" value={{index .Values 0}} maxlength="6"></div>

But of course it does not escape the Values in the html so XSS is a problem.

Update: For Cross site Scripting the Problem I want to show here is: If the json file contains the following

    "Values": [
    "\u003e",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    ""
]

which is the equivalent to >, the html printed will be

<div>Lampe0: <input type="text" name="Lampe0" value=> maxlength="6"></div>

So its not escaped

How to printf it into the html so its escaped?

Update2: The old working Version with escaping can be seen at https://github.com/Binary-Kitchen/gokitchenmood

  • 写回答

1条回答 默认 最新

  • drze7794 2014-07-08 12:04
    关注

    Ok it seems I was using text/template and not html/template. So it was basically and fault on my side. Thanks to JimB to point it out though.

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题