如果值为0,则输入值为空,否则为该值。

huangapple go评论79阅读模式
英文:

If value is 0 then input value will be empty, value otherwise

问题

问题:
我创建了一个简单的表单,其中有一个输入字段"num"。提交后,我希望在同一个输入字段中显示num的值,换句话说,希望保留该字段中的输入。如果值设置为0,则忽略该值。

我可以在几种语言中实现这个功能,但我不确定如何在Golang中实现。我的当前模板文件包含以下内容:

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

服务器文件包含以下内容:

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)
}

另一个想法是将N设置为字符串,即在服务器文件中将其设置为''或值。但这实际上破坏了变量的名称/用途。

有没有更好的方法来实现这个功能?有没有更好的方法直接从模板中访问GET参数?请注意,N的值最初是从一个GET变量中获取的。

*此代码未经过测试

英文:

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,

&lt;input type=&quot;text&quot; placeholder=&quot;foo&quot; name=&quot;bar&quot; value=&quot;{{if gt .N 0 }} {{.N}} {{end}} &quot;/&gt; 

Server file contains:

data := &amp;listOfReport {
		R: r,
		I: i,
		N: n
}
listTmpl := template.Must(template.New(&quot;list_tmpl&quot;).Parse(string(report.Template[&quot;xxx.tmpl&quot;])))
if err := listTmpl.Execute(w, data); err != nil {
	http.Error(w, fmt.Sprintf(&quot;Error rendering template %v&quot;, 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

答案1

得分: 2

在模板中,没有标准/内置的方法可以获取任何请求参数,你需要将其放入数据中。(你可以编写一个函数来为你完成这个操作,但这将导致一个丑陋的hack。)

我不明白你的解决方案有什么问题。

英文:

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.

答案2

得分: 1

我采用类似的方法,但使用结构体。

type SignupForm struct {
    Name   string
    Email  string
    Etcera bool
}

// 类型别名
type M map[string]interface{}

...

// 在接受表单的处理程序中
err := r.ParseForm()
if err != nil {
    // 处理错误
}

signup := SignupForm{}
err := decoder.Decode(signup, r.PostForm)
if err != nil {
    // 处理错误
}

// 将“保存”的表单内容存储在某个临时位置 -
// 例如:
// 	- cookies(请记住浏览器的4K限制)
//	- 服务器端会话(Redis;我是这样做的)
//	- 数据库

// 在呈现表单的处理程序中
err := template.ExecuteTemplate(w, "form.html", M{
    "form":      signup,
    "csrfToken": csrfToken,
    // 等等...
})

请注意,无论您将表单数据存储在何处,都要确保它是临时的。服务器端会话是理想的选择,因为您可以设置它们的过期时间(如果您不想手动删除它们)。

英文:

I take a similar approach, but use structs.

type SignupForm struct {
	Name string
	Email string
	Etcera bool
}

// Type alias
type M map[string]interface{}

...

// In the handler that accepts your form
err := r.ParseForm()
if err != nil {
	// handle error
}

signup := SignupForm{}
err := decoder.Decode(signup, r.PostForm)
if err != nil {
	// handle error
}

// Store the &#39;saved&#39; form contents somewhere temporary - 
// e.g.
// 	- cookies (keep in mind the 4K browser limit)
//	- server side sessions (Redis; how I do it)
//	- db

// In the handler that renders your form
err := template.ExecuteTemplate(w, &quot;form.html&quot;, M{
		&quot;form&quot;: signup,
		&quot;csrfToken&quot;: csrfToken,
		// and so on...
		})

Note that wherever you store the form data, make sure it is temporary. Server side sessions are ideal as you can have them expire (if you don't want to delete them manually).

huangapple
  • 本文由 发表于 2015年3月24日 15:36:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/29227149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定