go-templates

Golang template engine pipelines

南楼画角 提交于 2019-11-26 17:48:29
问题 I have a Golang template, defined like this {{- define "test" -}} {{- printf "%s" .Name | trunc 24 -}} {{- end -}} Then I use it in one of my files: {{ template "test" . }} What does the dot mean after "test"? Golang template docs say: {{template "name" pipeline}} The template with the specified name is executed with dot set to the value of the pipeline. But I am not sure what pipeline is. Reading documentation gave no results, could anyone explain once again? Also, why do we have to start

404 page not found - Go rendering css file

社会主义新天地 提交于 2019-11-26 17:19:35
问题 I'm currently working in Go. I created a web server on my local machine. I followed the instruction on this page Rendering CSS in a Go Web Application but I'm still getting the 404 error that the program can't seem to find where my css file is. My directory is as follows. In src folder contains css/somefilename.css , src also contains server/server.go . The code inside my server.go file is as follows. http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css")))) When I go

In a Go template range loop, are variables declared outside the loop reset on each iteration?

安稳与你 提交于 2019-11-26 14:47:15
问题 I'm trying to use a variable declared outside a Go template range loop to see if the previous post occurred on the same day as the current post. Here's a simplified example. Where .Posts is an array of post structs that each have a .Content and a .Date . {{ $prevDate := "" }} {{ range $post := .Posts }} {{ if ne $prevDate $post.Date }} <div class="post-date">Posts dated: {{ $post.Date }}</div> {{ end }} <div class="post-content">{{ $post.Content }}</div> {{ $prevDate := $post.Date }} {{ end }

Golang templates (and passing funcs to template)

北慕城南 提交于 2019-11-26 14:46:10
问题 I'm getting an error when I try and access a function I'm passing to my template: Error: template: struct.tpl:3: function "makeGoName" not defined Can anyone please let me know what I'm doing wrong? Template file (struct.tpl): type {{.data.tableName}} struct { {{range $key, $value := .data.tableData}} {{makeGoName $value.colName}} {{$value.colType}} `db:"{{makeDBName $value.dbColName}},json:"{{$value.dbColName}}"` {{end}} } Calling file: type tplData struct { tableName string tableData

Go template.ExecuteTemplate include html

风流意气都作罢 提交于 2019-11-26 14:02:08
问题 I have followed this tutorial: http://golang.org/doc/articles/wiki/final.go and have slightly modified it for my needs/wants. The problem is I would like to support HTML in the templates. I realize this is a security risk but it's not a concern at the moment. The result of a page render: <h1>this<strong>is</strong>a test</h1> Let me explain a little bit of the code: type Page struct { Title string Body []byte } The data I would like to have HTML is stored in Page.Body . This is type []byte

Calling a template with several pipeline parameters

不羁的心 提交于 2019-11-26 10:34:53
问题 In a Go template, sometimes the way to pass the right data to the right template feels awkward to me. Calling a template with a pipeline parameter looks like calling a function with only one parameter. Let\'s say I have a site for Gophers about Gophers. It has a home page main template, and a utility template to print a list of Gophers. http://play.golang.org/p/Jivy_WPh16 Output : *The great GopherBook* (logged in as Dewey) [Most popular] >> Huey >> Dewey >> Louie [Most active] >> Huey >>

How to use a field of struct or variable value as template name?

耗尽温柔 提交于 2019-11-26 08:28:17
问题 We can define template name via {{define \"home\"}} , and then load it in other (parent) template via {{template \"home\"}} . How I can load template via variable value {{template .TemplateName}} . Or it\'s impossible? 回答1: Unfortunately you can't. The syntax of the {{template}} action: {{template "name"}} The template with the specified name is executed with nil data. {{template "name" pipeline}} The template with the specified name is executed with dot set to the value of the pipeline. The