英文:
HTML Form Submission in golang template
问题
我正在使用Go语言和go-template来提交一个HTML表单,但是得到了一些奇怪的结果。
基本思路是我有一个名为Page的数据结构,其中包含一些元素。模板使用一个Page数组进行填充。在模板中,我遍历每个Page并显示其内容。每个内容都嵌入在一个带有相应链接的HTML表单中。一旦点击链接,它将提交相应的表单。
代码片段如下:
{{range $index, $element := .Pages}}
<form action="/detailNews" id="readMore{{$index}}" method="post" name="readMore{{$index}}">
//显示每个页面的元素
<div id="more">
<input name="query" type="hidden" value="{{printf "%s" .Title}}">
<a href="#" onclick="document.readMore{{$index}}.submit()">阅读更多</a>
</div>
</form>
{{end}}
这段代码大部分都能正常工作,只有一个小问题。id和name属性生成的输出如预期的那样,例如:readMore0,readMore1等。
问题出在"a"标签的onclick属性上,它的值被填充为:document.readMore 0 .submit(),document.readMore 1 .submit()等。请注意0和1周围的空格。由于这个问题,当点击链接时,无法找到相应的表单。
我无法找出原因。
非常感谢您的帮助。
谢谢,
Ripul
英文:
I am using the go language to submit an HTML form using the go-template. Getting some weird results.
The basic idea is that I have a data structure called Page containing a few elements. The template is populated with an array of Pages. Inside the template I iterate through each Page and display its contents. Each of these contents are embedded inside an HTML form with its respective link. Once the link is clicked it will submit the respective form.
The code snippet is as follows:
{{range $index, $element := .Pages}}
<form action="/detailNews" id="readMore{{$index}}" method="post" name="readMore{{$index}}">
//displaying elements from each page
<div id="more">
<input name="query" type="hidden" value="{{printf "%s" .Title}}">
<a href="#" onclick="document.readMore{{$index}}.submit()">Read More</a>
</div>
</form>
{{end}}
The code mostly works with one little problem. The id and the name attributes generate outputs as expected such as: readMore0, readMore1, etc.
The problem is at the "a" tag where the onclick attribute is populated with this: document.readMore 0 .submit(), document.readMore 1 .submit(), etc. Note the space surrounding 0, 1. With this, the respective form is not being found when the link is clicked.
I can't figure out the reason of this.
Any help will be highly appreciated.
Thanks,
Ripul
答案1
得分: 6
这是由于转义上下文的原因。
默认情况下,该软件包假定所有流水线都生成纯文本字符串。它会添加必要的转义流水线阶段,以正确且安全地嵌入该纯文本字符串到适当的上下文中。
当数据值不是纯文本时,您可以通过标记其类型来确保它不会过度转义。
来自content.go的HTML、JS、URL和其他类型可以携带安全内容,免于转义。
为了解决这个问题,您应该将页面索引作为.Pages
元素结构的一部分传递,并将其类型设置为template.JS
。类似于以下代码:
type Page struct {
Id template.JS
Title string
}
英文:
This is happening because of escaping contexts.
> By default, this package assumes that all pipelines produce a plain
> text string. It adds escaping pipeline stages necessary to correctly
> and safely embed that plain text string in the appropriate context.
>
> When a data value is not plain text, you can make sure it is not
> over-escaped by marking it with its type.
>
> Types HTML, JS, URL, and others from content.go can carry safe content
> that is exempted from escaping.
In order to overcome it you should pass the page index as part of the .Pages
element structs typed as template.JS
. Something like this :
type Page struct {
Id template.JS
Title string
}
答案2
得分: 3
你好,以下是翻译好的内容:
你有一个可以重现的工作示例吗?我在本地尝试过,结果如预期:
package main
import (
"os"
"text/template"
)
type Page struct {
Title string
}
func main() {
template.Must(template.New("test").Parse(`
{{range $index, $element := .Pages}}
<form action="/detailNews" id="readMore{{$index}}" method="post" name="readMore{{$index}}">
//从每个页面显示元素
<div id="more">
<input name="query" type="hidden" value="{{printf "%s" .Title}}">
<a href="#" onclick="document.readMore{{$index}}.submit()">Read More</a>
</div>
</form>
{{end}}
`)).Execute(os.Stdout, struct{ Pages []Page }{Pages: []Page{
{Title: "page1"}, {Title: "page2"},
}})
}
结果:
<form action="/detailNews" id="readMore0" method="post" name="readMore0">
//从每个页面显示元素
<div id="more">
<input name="query" type="hidden" value="page1">
<a href="#" onclick="document.readMore0.submit()">Read More</a>
</div>
</form>
<form action="/detailNews" id="readMore1" method="post" name="readMore1">
//从每个页面显示元素
<div id="more">
<input name="query" type="hidden" value="page2">
<a href="#" onclick="document.readMore1.submit()">Read More</a>
</div>
</form>
英文:
Do you have a working example to reproduce? I tried locally and it works as expected:
http://play.golang.org/p/ZnM0RqIMfL
package main
import (
"os"
"text/template"
)
type Page struct {
Title string
}
func main() {
template.Must(template.New("test").Parse(`
{{range $index, $element := .Pages}}
<form action="/detailNews" id="readMore{{$index}}" method="post" name="readMore{{$index}}">
//displaying elements from each page
<div id="more">
<input name="query" type="hidden" value="{{printf "%s" .Title}}">
<a href="#" onclick="document.readMore{{$index}}.submit()">Read More</a>
</div>
</form>
{{end}}
`)).Execute(os.Stdout, struct{ Pages []Page }{Pages: []Page{
{Title: "page1"}, {Title: "page2"},
}})
}
Result:
<form action="/detailNews" id="readMore0" method="post" name="readMore0">
//displaying elements from each page
<div id="more">
<input name="query" type="hidden" value="page1">
<a href="#" onclick="document.readMore0.submit()">Read More</a>
</div>
</form>
<form action="/detailNews" id="readMore1" method="post" name="readMore1">
//displaying elements from each page
<div id="more">
<input name="query" type="hidden" value="page2">
<a href="#" onclick="document.readMore1.submit()">Read More</a>
</div>
</form>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论