This commit is contained in:
github-actions[bot]
2022-08-04 07:15:39 +00:00
parent ca571eb691
commit b706f63726
34 changed files with 9 additions and 60436 deletions

View File

@@ -172,9 +172,7 @@ Title: {{.Title | printf "%.64s"}}
Age: {{.CreatedAt | daysAgo}} days
{{end}}`
</code></pre>
<p>{% endraw %}</p>
<p>这个模板先打印匹配到的issue总数然后打印每个issue的编号、创建用户、标题还有存在的时间。对于每一个action都有一个当前值的概念对应点操作符写作“.”。当前值“.”最初被初始化为调用模板时的参数在当前例子中对应github.IssuesSearchResult类型的变量。模板中<code>{{.TotalCount}}</code>对应action将展开为结构体中TotalCount成员以默认的方式打印的值。模板中<code>{{range .Items}}</code><code>{{end}}</code>对应一个循环action因此它们之间的内容可能会被展开多次循环每次迭代的当前值对应当前的Items元素的值。</p>
<p>{% endraw %}</p>
<p>在一个action中<code>|</code>操作符表示将前一个表达式的结果作为后一个函数的输入类似于UNIX中管道的概念。在Title这一行的action中第二个操作是一个printf函数是一个基于fmt.Sprintf实现的内置函数所有模板都可以直接使用。对于Age部分第二个动作是一个叫daysAgo的函数通过time.Since函数将CreatedAt成员转换为过去的时间长度</p>
<pre><code class="language-Go">func daysAgo(t time.Time) int {
return int(time.Since(t).Hours() / 24)
@@ -247,7 +245,6 @@ var issueList = template.Must(template.New(&quot;issuelist&quot;).Parse(`
&lt;/table&gt;
`))
</code></pre>
<p>{% endraw %}</p>
<p>下面的命令将在新的模板上执行一个稍微不同的查询:</p>
<pre><code>$ go build gopl.io/ch4/issueshtml
$ ./issueshtml repo:golang/go commenter:gopherbot json encoder &gt;issues.html
@@ -275,7 +272,6 @@ $ ./issueshtml repo:golang/go commenter:gopherbot json encoder &gt;issues.html
}
}
</code></pre>
<p>{% endraw %}</p>
<p>图4.6显示了出现在浏览器中的模板输出。我们看到A的黑体标记被转义失效了但是B没有。</p>
<p><img src="../images/ch4-06.png" alt="" /></p>
<p>我们这里只讲述了模板系统中最基本的特性。一如既往,如果想了解更多的信息,请自己查看包文档:</p>