GO模板用于动态HTML

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

GO templates for dynamic html

问题

有人可以告诉我如何使用GO模板编写类似https://developers.google.com/appengine/docs/java/gettingstarted/usingjsps上的guestbook.jsp这样的简单页面吗?

在我看来,编写一个能工作的页面很容易,但我想知道是否可以像JSP页面一样简洁地完成。

有两个问题我不知道如何解决:

  1. JSP页面使用了两个对象user和request,而模板创建只需要一个对象。
  2. 如果我们使用单独的模板来生成登出链接和留言板,那么如何将它们嵌套在主页面中?
英文:

Can anyone please tell me how to code up simple page like guestbook.jsp on https://developers.google.com/appengine/docs/java/gettingstarted/usingjsps using GO templates?

In my opinion it is easy to write one that works but I would like to know if it can be done as concisely as a JSP page.

There are two problems that I don't know how to address

  1. JSP page uses two objects user and request while template creation takes only one.
  2. If we use separate templates for generating a sign-out link and a guestbook, then how do we nest them in the main page?

答案1

得分: 11

我所做的是创建一个结构体(我称之为“页面”对象),用所需的实体填充它,然后在模板中对它们进行操作。

func myPage(w http.ResponseWriter, r *http.Request) {
    var user *User // 从某处获取
    
    page := struct {		
        Title     string
        User      *User
    }{"My title", user}

    return templates.ExecuteTemplate(w, "myPage", page)
}

模板可以像这样,让您可以访问结构体中的所有字段:

{{define "myPage"}}
{{template "head" .}}

Title: {{.Title}}<br />
Name: {{.User.Name}}<br />

{{template "tail" .}}
{{end}}

(请注意,{{template "head" .}}将包含另一个模板,这里是一个头部和一个尾部。)

我经常使用的另一件事是模板中的变量。您可以使用美元符号定义变量。

以下示例不太优雅,但可能会让您了解可能的情况。假设我们有三个切片:一个包含“User”对象,一个包含“Spot”对象,一个包含“Checkin”对象。它们的长度都相同,并且通过位置(索引0表示签到的用户,索引0表示签到的地点,签到对象包含签到发生的时间)相关联。range在迭代切片时会给您两个变量:索引(在示例中为$i)和值($v)。使用“index”您可以请求切片中的实体,因此{{$user := index $checkinUsers $i}}将给您指向$i所指示位置的对象。

{{$checkinUsers := .CheckinUsers}}
{{$checkinSpots := .CheckinSpots}}
{{range $i, $v := .Checkins}}
    {{$user := index $checkinUsers $i}}
    {{$spot := index $checkinSpots $i}}
    <tr>
        <td>
            {{$user.FirstName}} {{$user.LastName}} @ {{$spot.Description}} ({{$v.Time}})<br />
        </td>
    </tr>
{{end}}

再次说明,这个示例不太优雅,但我希望您能看到在Go中制作动态HTML与在JSP中一样容易(根据我的经验,生成的页面比JSP更清晰,因此对于经验较少的Web开发人员和设计师更易于理解)。

祝好,
/Alexander Yngling.

英文:

What I do is create a struct (that I call a "page" object), populate it with the entities I need, and then operate on them in the template.

func myPage(w http.ResponseWriter, r *http.Request) {
    var user *User // fetch from somewhere

    page := struct {		
        Title     string
        User      *User
    }{&quot;My title&quot;, user}

    return templates.ExecuteTemplate(w, &quot;myPage&quot;, page)
}

The template can look something like this, giving you access to all fields in the struct:

{{define &quot;myPage&quot;}}
{{template &quot;head&quot; .}}

Title: {{.Title}}&lt;br /&gt;
Name: {{.User.Name}}&lt;br /&gt;

{{template &quot;tail&quot; .}}
{{end}}

(Note that {{template &quot;head&quot; .}} will include another template, here a header and a footer.)

Another thing I use a lot are variables in the templates. You can define variables by using the dollar symbol.

The following example is not very elegant, but may give you an idea of what is possible. Imagine we have three slices: one with "User" objects, one with "Spot" objects and one with "Checkin" objects. They are all the same length and are related by position (index 0 for each contains the user that was checked in, the spot he/she checked in at, and the checkin object contains the time it happened). "range" will give you two variables as it iterates over the slice: index ($i in the example) and the value ($v). Using "index" you can ask for an entity in a slice, so {{$user := index $checkinUsers $i}} will give you the object at the position pointed to by $i.

{{$checkinUsers := .CheckinUsers}}
{{$checkinSpots := .CheckinSpots}}
{{range $i, $v := .Checkins}}
    {{$user := index $checkinUsers $i}}
    {{$spot := index $checkinSpots $i}}
    &lt;tr&gt;
        &lt;td&gt;
            {{$user.FirstName}} {{$user.LastName}} @ {{$spot.Description}} ({{$v.Time}})&lt;br /&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    {{end}}

Again, this example isn't very elegant, but I hope you can see that it is possible to make dynamic HTML in Go just as easily as in JSPs (my experience is that the resulting pages are cleaner than JSPs, and thus more understandable by less experienced web-developers and designers).

Ex animo,
/Alexander Yngling.

huangapple
  • 本文由 发表于 2012年6月9日 17:38:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/10959912.html
匿名

发表评论

匿名网友

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

确定