我想知道是否可以使用自定义函数作为 Golang 模板的模板块。下面的代码显示了一个示例。
{{ custom_func . }}
This is content that "custom_func" should do something with.
{{ end }}
用例有点特殊且不标准。基本上,我希望模板作者能够传入大块文本,其中换行符等受到尊重,并将整个文本块传递给函数。我本可以做类似的事情:
{{ custom_func "This is a lot of text\n with many lines etc." }}
但这对模板作者来说不是很友好。最终目标是让他们写出这样的东西:
Author is writing something normal...
{{ note }}
But would like to wrap this content as a "note".
Which when passed to the "note" function, will wrap the content with appropriate divs etc.
{{ end }}
基本上我正在尝试一个实验,看看我是否可以使用纯 go 模板实现类似“markdown/reStructuredText”的内容。现在主要是一个实验。
最终我可能需要为此编写一个合适的 PEG 解析器,但我想先看看这是否可行。
森栏
LEATH
随时随地看视频慕课网APP
相关分类