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

在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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建