Go: display array in array with templates

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

Go: display array in array with templates

问题

如何在Go模板中插入变量,就像这样 - 我在HTML中有这段代码:

{{define "homepage"}}
<html>
<form action="/home/delete" method="POST">
    {{with .Posts}}
        {{range .}} 
            <p id="twt">{{ range $i := .Status}}{{$i}}<br><br></p>
            <button type="submit" id="xbutton" name="xdel" value="{{.Tweetid}}">Delete</button>
        {{end}}
    {{end}}
</form>
</html>
{{end}}

Go代码如下:

type User struct {
    Userid    	int64
    Username  	string
    Password 	string
    Posts 		[]*Post
}

type Post struct {
    Tweetid 	int
    Username    string
    Status 		[]string
}

func deletehandler(w http.ResponseWriter, r *http.Request) {
    currentuser = getUserName(r)
    postvalue = r.PostFormValue("xdel")
    DeleteTweet() 
    if currentuser != "" {
        as := Post{Username: currentuser, Status: ReadStatus(), Tweetid: ReadStatusId()}
        person := User{Username: currentuser, Posts: []*Post{&as}}
        t := template.Must(template.New("tele").ParseFiles("layout/home.html"))
        if err := t.ExecuteTemplate(w, "homepage", person); err != nil {
            panic(err)
        }
    } else {
        http.Redirect(w, r, "/", 302)
    }
}

//获取.Tweetid
func ReadStatusId() (res int) {
    //打开和访问SQL数据库的一些代码
    rows, _ := db.Query("Select id from posts where tweet = ?", AddTweet)
    //一些错误处理的代码
    defer rows.Close()

    var status string
    for rows.Next() {
        err := rows.Scan(&status)
        if err != nil {
            fmt.Println(err)
        }
        fmt.Printf("this %s", status)
    }
    return
}

func main() {
    //(其他处理程序的代码...)
    router.HandleFunc("/home/delete", deletehandler).Methods("POST")
}

然而,我得到的错误消息是:

can't evaluate field Tweetid in type string

如何修复这个问题并允许在值字符串中读取.Tweetid?如果有帮助的话,我参考了这个链接中的模板用法:http://jan.newmarch.name/go/template/chapter-template.html

英文:

How do I insert a variable in a Go template like this - I have this code in HTML:

{{define &quot;homepage&quot;}}
&lt;html&gt;
&lt;form action=&quot;/home/delete&quot; method=&quot;POST&quot;&gt;
{{with .Posts}}
{{range .}} 
&lt;p id=&quot;twt&quot;&gt;{{ range $i := .Status}}{{$i}}&lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;button type=&quot;submit&quot; id=&quot;xbutton&quot; name=&quot;xdel&quot; value=&quot;{{.Tweetid}}&quot;&gt;Delete&lt;/button&gt;
{{end}}
{{end}}
{{end}}
&lt;/form&gt;
&lt;/html&gt;
{{end}}

The code in Go:

type User struct {
Userid    	int64
Username  	string
Password 	string
Posts 		[]*Post
}
type Post struct {
Tweetid 	int
Username    string
Status 		[]string
}
func deletehandler(w http.ResponseWriter, r *http.Request) {
currentuser = getUserName(r)
postvalue = r.PostFormValue(&quot;xdel&quot;)
DeleteTweet() 
if currentuser != &quot;&quot; {
as := Post{Username: currentuser, Status: ReadStatus(), Tweetid: ReadStatusId()}
person := User{Username: currentuser, Posts: []*Post{&amp;as}}
t := template.Must(template.New(&quot;tele&quot;).ParseFiles(&quot;layout/home.html&quot;))
if err := t.ExecuteTemplate(w, &quot;homepage&quot;, person); err != nil {
panic(err)
}
} else {
http.Redirect(w, r, &quot;/&quot;, 302)
}
}
//getting the .Tweetid
func ReadStatusId() (res int) {
//some code to open and access the sql database
rows, _ := db.Query(&quot;Select id from posts where tweet = ?&quot;, AddTweet)
some code for error handling
defer rows.Close()
var status string
for rows.Next() {
err := rows.Scan(&amp;status)
if err != nil {
fmt.Println(err)
}
fmt.Printf(&quot;this %s&quot;, status)
}
return
}
func main() {
(//code for other handlers..)
router.HandleFunc(&quot;/home/delete&quot;, deletehandler).Methods(&quot;POST&quot;)
}

However, the error message I get is

can&#39;t evaluate field Tweetid in type string

How do I fix this and allow .Tweetid to be read in the value string? If this helps I referred to this for the way I've used the templates: http://jan.newmarch.name/go/template/chapter-template.html

答案1

得分: 3

尝试更改模板:

{{range $p := .Posts}} 
&lt;p id=&quot;twt&quot;&gt;{{ range $i := .Status}}{{$i}}&lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;button type=&quot;submit&quot; id=&quot;xbutton&quot; name=&quot;xdel&quot; value=&quot;{{$p.Tweetid}}&quot;&gt;删除&lt;/button&gt;
{{end}}
{{end}}

{{ range $i := ... }} 中使用 {{.Tweetid}} 将引用 {{$i.Tweetid}},添加 range $p := .Posts}} 并明确使用 {{$p.Tweetid}} 引用它将解决问题。

英文:

Try to change the template:

{{range $p := .Posts}} 
&lt;p id=&quot;twt&quot;&gt;{{ range $i := .Status}}{{$i}}&lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;button type=&quot;submit&quot; id=&quot;xbutton&quot; name=&quot;xdel&quot; value=&quot;{{$p.Tweetid}}&quot;&gt;Delete&lt;/button&gt;
{{end}}
{{end}}

using {{.Tweetid}} inside the {{ range $i := ... }} would reference {{$i.Tweetid}} adding range $p := .Posts}} and referencing it explicitly with {{$p.Tweetid}} will solve the problem

huangapple
  • 本文由 发表于 2014年8月18日 23:53:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/25367310.html
匿名

发表评论

匿名网友

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

确定