Michée Lengronne
Michée Lengronne

Reputation: 789

How to ignore elements in go template/text

I have a file that I convert through go template/text. The file contains a line that I want the template parser to ignore. This line contains keys with a very similar syntax than the template ones and it confuses the parser. It tries to interpret them but it should not.

Here is the confusing line:

GRAVATAR_SOURCE = https://{{ .Env.GRAVATARHOST }}
# Line I would like go template/text to ignore
ACCESS_LOG_TEMPLATE = {{.Ctx.RemoteAddr}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}\" \"{{.Ctx.Req.UserAgent}}"

As you can see, GRAVATAR_SOURCE is succesfully parsed by go template/text but ACCESS_LOG_TEMPLATE breaks as it shouldn't be parsed (the template in this line being for a log parser).

How can I tell go template/text to ignore that line ?

I would like to use something equivalent to {% raw %}{% endraw %} in jinja2. That way, I can use any go binaries that have a template/text parser without the need to alter and recompile it.

Thanks.

Upvotes: 0

Views: 3594

Answers (2)

Michée Lengronne
Michée Lengronne

Reputation: 789

I think I found my answer.

According to https://golang.org/pkg/text/template/#hdr-Examples

I can wrap my ACCESS_LOG_TEMPLATE with:

{{`"output"`}}

That way:

GRAVATAR_SOURCE = https://{{ .Env.GRAVATARHOST }}
# Line I would like go template/text to ignore
{{`ACCESS_LOG_TEMPLATE = {{.Ctx.RemoteAddr}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}\" \"{{.Ctx.Req.UserAgent}}"`}}

Upvotes: 5

icza
icza

Reputation: 418127

You may change the delimeters of what needs to be parsed, so the rest will be "ignored". You may use the Template.Delims() method for that.

For example:

t := template.Must(template.New("").Delims("[[", "]]").Parse(src))
m := map[string]interface{}{
    "Env": map[string]interface{}{
        "GRAVATARHOST": "xx",
    },
}
if err := t.Execute(os.Stdout, m); err != nil {
    panic(err)
}

const src = `GRAVATAR_SOURCE = https://[[ .Env.GRAVATARHOST ]]
# Line I would like go template/text to ignore
ACCESS_LOG_TEMPLATE = {{.Ctx.RemoteAddr}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}\" \"{{.Ctx.Req.UserAgent}}"
`

This will output (try it on the Go Playground):

GRAVATAR_SOURCE = https://xx
# Line I would like go template/text to ignore
ACCESS_LOG_TEMPLATE = {{.Ctx.RemoteAddr}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}\" \"{{.Ctx.Req.UserAgent}}"

Upvotes: 4

Related Questions