问题描述
在golang中,是否可以截断html模板中的文本?
例如,我的模板中包含以下内容:
{{range .SomeContent}}
....
{{.Content}}
... 。
{{end}
{{ .Content}}
产生:Interdum et malesuada fames ac ante ipsum primis in faucibus。 Aliquam tempus sem ipsum,vel accumsan felis vulputate id。 Donec ultricies sem purus,non aliquam orci dignissim et。整数简历。 Pellentesque a ipsum quis velit venenatis vulputate vulputate ut enim。
我想将其缩减为25个字符。
fmt.Sprintf
。在你的情况下,截断一个字符串可能很简单: {{printf \%。25s \。内容}}
In golang, is there a way to truncate text in an html template?
For example, I have the following in my template:
{{ range .SomeContent }}
....
{{ .Content }}
....
{{ end }
{{ .Content }}
produces: Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam tempus sem ipsum, vel accumsan felis vulputate id. Donec ultricies sem purus, non aliquam orci dignissim et. Integer vitae mi arcu. Pellentesque a ipsum quis velit venenatis vulputate vulputate ut enim.
I would like to reduce that to 25 characters.
You can use printf
in templates, which acts as fmt.Sprintf
. In your case truncating a string would be as easy as:
"{{ printf \"%.25s\" .Content }}"
这篇关于如何截断Golang模板中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!