如何在模板的静态部分包含“{”或“}”时执行Golang模板?

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

How to execute a Golang template when "{" or "}" are in the static part of the template?

问题

我的问题是,我想建立一个信件生成器,首先根据用户输入构建一个latex文件,然后通过latex编译成PDF。

模板中包含多行类似于这样的内容:

\setkomavar{signature}{{{.Name}}}

其中latex部分是\setkomavar{signature}{},而go语言的template部分是{{.Name}}

当我尝试加载模板时,它抛出以下错误:

panic: template: letter.tmpl:72: unexpected "}" in command

有没有什么技巧可以帮助解析器处理这种情况?

提前感谢,

Tino

英文:

My Problem is that, I want to build a letter generator, which first builds a latex-file from user input, and then compiles this via latex to PDF.

The template contains multiple lines like this:

\setkomavar{signature}{{{.Name}}}

The latex part is \setkomavar{signature}{}, and the template part from go is {{.Name}}.

When I try to load the template, it throws this error:

panic: template: letter.tmpl:72: unexpected "}" in command

Is there a trick to help the parser handling such a situation?

Thanks in advance,

Tino

答案1

得分: 16

使用Template.Delims来将定界符设置为一些不冲突的文本。{{}}只是默认值,这种方法允许选择其他定界符。

另一种方法:在模板中,当你想要使用latex的{}时,可以插入一些安全的文本,比如#()#,然后对模板的输出进行“全局”替换。然而,设置定界符在我看来要容易得多,而且很可能更高效,如果这很重要的话。

英文:

Use Template.Delims to set the delimiters to some non conflicting text. {{ and }} are just the default values, this method allows to select other delimiters.

Alternative method: In you template, where you want latex's { and }, you can insert some safe text instead, like say #( and )# and then make a "global" replacement on the output from the template. Yet setting delimiters is far easier IMO and quite probably more performant, if that matters.

答案2

得分: 2

一个更好的解决方案实际上是只使用内置的空白符操作符,例如:

\setkomavar{signature}{ {{- .Name -}} }

开始和结束的-将删除该标记与下一个非模板标记之间的空白。

希望这有所帮助,更多详细信息请参阅文档

英文:

A better solution is actually to just use the built in whitespace operators, like:

\setkomavar{signature}{ {{- .Name -}} }

The - at the beginning and end will remove whitespace between that token and the next non-template token.

Hope that helps, see the docs for more detail

答案3

得分: 0

我之前是通过创建模板函数来实现这个功能的:

<!-- begin snippet: js hide: false -->

<!-- language: lang-golang -->

func texArg(s interface{}) string {
	return fmt.Sprintf(&quot;{%v}&quot;, s)
}

<!-- end snippet -->

我使用template.Funcs将其注册为arg。然后在我的模板中使用:

<!-- begin snippet: js hide: false -->

<!-- language: lang-golang -->

\textbf{{.Name | arg}}

<!-- end snippet -->

我认为@zzzz的答案更好,因为当你需要嵌套时,这种方法会出现问题,但我还是想把这个留在这里作为另一种方法。

英文:

I was previously doing this by creating the template function:

<!-- begin snippet: js hide: false -->

<!-- language: lang-golang -->

func texArg(s interface{}) string {
	return fmt.Sprintf(&quot;{%v}&quot;, s)
}

<!-- end snippet -->

which I registered as arg using template.Funcs. Then in my template I had:

<!-- begin snippet: js hide: false -->

<!-- language: lang-golang -->

\textbf{{.Name | arg}}

<!-- end snippet -->

I think @zzzz's answer above is better as this falls apart when you need to nest it, but I thought I'd leave this here for an alternative approach.

答案4

得分: 0

我的临时解决方案是:

\barcode{{print &quot;{&quot; .Barcode}}}
英文:

my adhok solution was:

\barcode{{print &quot;{&quot; .Barcode}}}

huangapple
  • 本文由 发表于 2013年5月23日 15:03:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/16707600.html
匿名

发表评论

匿名网友

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

确定