Golang模板文件中的重定向函数失败了。

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

Golang Redirect function inside template file fails

问题

根据golang文档的说明,模板函数必须至少有一个返回参数,所以我认为可以排除模板函数的可能性。

所以我想我可以直接在Content模型上创建函数,但我猜不行?

我认为下面的标记很容易理解(根文件夹中的index.tmpl文件):

{{$request := .Request}}
{{$response := .Response}}
{{if eq $request.Method "POST"}}
	{{.Redirect "page1" 301}}
{{end}}

<!DOCTYPE html>
<html>
  <head>
    <title>{{.Title | title}}</title>
  </head>
  <body>
    <h1>{{.Title}}</h1>
    {{.Body}}
    <p>{{.ContentType}}</p>
    <p>{{$request}}</p>
    <p>{{lol}}</p>
    <form method="POST">
    	<input type="text" name="lolcat"/>
    	<input type="submit" value="submit"/>
    </form>
  </body>
</html>

这在某种程度上等同于在ASP.Net/Razor中的做法,但它不起作用。

其余的代码在这里:http://play.golang.org/p/LMKdflA9RS

要在本地测试该程序,你需要有:

main.go
index.tmpl
page1.tmpl(只需使用与index.tmpl相同的内容)

我做错了什么,如何实现这个? Golang模板文件中的重定向函数失败了。

解决方案:

http://play.golang.org/p/b2I_uzjiSH

感谢Ainar-G!

英文:

As the golang documentation states, a template function has to have at least 1 return parameter, so I think I can rule out template functions already.

So I thought I could just create the function directly on the Content model, but I guess not?

I think the markup below is pretty self explaining (index.tmpl file in root folder):

{{$request := .Request}}
{{$response := .Response}}
{{if eq $request.Method "POST"}}
	{{.Redirect "page1" 301}}
{{end}}

<!DOCTYPE html>
<html>
  <head>
    <title>{{.Title | title}}</title>
  </head>
  <body>
    <h1>{{.Title}}</h1>
    {{.Body}}
    <p>{{.ContentType}}</p>
    <p>{{$request}}</p>
    <p>{{lol}}</p>
    <form method="POST">
    	<input type="text" name="lolcat"/>
    	<input type="submit" value="submit"/>
    </form>
  </body>
</html>

This is kinda equivalent to how one would do it in ASP.Net/Razor, but it doesn't work.

The rest of the code is here http://play.golang.org/p/LMKdflA9RS

To test the program locally you need to have:

main.go
index.tmpl
page1.tmpl (just use the same as index.tmpl)

What am I doing wrong, and how can this be achieved? Golang模板文件中的重定向函数失败了。

Solution:

http://play.golang.org/p/b2I_uzjiSH

Thanks to Ainar-G!

答案1

得分: 6

这样的逻辑应该放在处理程序中。模板只用于呈现数据,不设计其他功能。关于何时以及如何呈现的决策应该在处理程序代码中完成。

考虑到这一点,以下是你可以实现的方法:http://play.golang.org/p/Md5A54rLm7。

你不能直接写入w,因为这会导致它将响应代码设置为200。你还应该在包装器中注册redirect函数,因为所有模板函数都必须返回一个或两个(带有错误)值。再次强调,仅仅因为你可以这样做,并不意味着你应该这样做。

英文:

Logic like this should go into handlers. Templates are for presenting data only. They are not designed to be anything more. Any decisions about what and how to render should be done in the handler code.

With that in mind, here is how you could do that: http://play.golang.org/p/Md5A54rLm7.

You can't write directly to w because this will cause it to set response code to 200. You should also register the redirect function inside a wrapper because all template functions must return one or two (with error) values. Again, just because you can, it doesn't mean you should.

huangapple
  • 本文由 发表于 2015年7月17日 18:58:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/31474263.html
匿名

发表评论

匿名网友

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

确定