dongyao1915 2014-03-13 00:26
浏览 484
已采纳

在golang html模板中访问{{range。}}范围之外的struct变量

<!DOCTYPE html>
<html>
<head>
    <title> Test </title>
</head>
<body>
    <div>
        <h2>Reply</h2>
        <form action="/post/{{$threadID}}" method="POST">
        <input type="text" name="subject" />
        <input type="text" name="name" value="Anonymous" />
        <input type="text" name="message" />
        <input type="submit" value="submit" />
        </form>
    </div>
    <div>
        {{range .}}
        {{$threadID := .ThreadID}}
        <h3>{{.Subject}}</h3>
        <h3>{{.Name}}</h3>
        <div>{{.DatePosted}}</div>
        <div><p>{{.Text}}</p></div>
        <br /><br />
        {{end}}
    </div>
</body>

I have this template, there is a form at the top of the page that requires the threadID from ANY one of the Posts sent (they're all the same, a slice of all posts with a certain threadID), this obviously doesn't work, my only other idea was something along the lines of

{{range .}}
    {{if $threadID == nil}}
        $threadID := .ThreadID
        //build the form same as above
    {{end}}
    <h3>{{.Subject}}</h3>
    <h3>{{.Name}}</h3>
    <div>{{.DatePosted}}</div>
    <div><p>{{.Text}}</p></div>
    <br /><br />
{{end}}

Here is the Post structure and methods if any of the above is unclear.

type Post struct {
threadID int
subject string
name string
text string
date_posted string
}

func (p *Post) ThreadID()   int    { return p.threadID    }
func (p *Post) Subject()    string { return p.subject     }
func (p *Post) Name()       string { return p.name        }
func (p *Post) Text()       string { return p.text        }
func (p *Post) DatePosted() string { return p.date_posted } 

And the origin of the slice of posts sent to the template

threadID := r.URL.Path[len("/reply/"):]
replies, err := i.db.Query("SELECT * FROM latest_threads where thread_id="+threadID);
  • 写回答

2条回答 默认 最新

  • dragon4587 2014-03-13 01:38
    关注

    You can pass it all as one struct like so:

    layoutData := struct {
        ThreadID int
        Posts []Post
    } {
        ThreadID: threadID,
        Posts: Posts,
    }
    

    Then something like this will work

    <!DOCTYPE html>
    <html>
    <head>
        <title> Test </title>
    </head>
    <body>
        <div>
            <h2>Reply</h2>
            <form action="/post/{{ .ThreadID }}" method="POST">
            <input type="text" name="subject" />
            <input type="text" name="name" value="Anonymous" />
            <input type="text" name="message" />
            <input type="submit" value="submit" />
            </form>
        </div>
        <div>
            {{range $post := .Posts}}
            <h3>{{ $post.Subject}}</h3>
            <h3>{{$post.Name}}</h3>
            <div>{{$post.DatePosted}}</div>
            <div><p>{{$post.Text}}</p></div>
            <br /><br />
            {{end}}
        </div>
    </body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里