go - 如何修剪 Go 模板中的空白
问题描述
我想修剪 Go 模板中的空白。我该怎么做?
例子:
{{ $title = " My Title of the product " }}
// Print the trim string here
<h1>{{ $title }}</h1>
解决方案
没有任何内置功能可以在模板内为您修剪字符串“管道”,但是strings.TrimSpace
如果您使用该方法将其提供给该模板,则可以在模板内使用该函数Funcs
。
var str = `{{ $title := " My Title of the product " }}
// Print the trim string here
<h1>{{ trim $title }}</h1>`
t := template.Must(template.New("t").Funcs(template.FuncMap{
"trim": strings.TrimSpace,
}).Parse(str))
推荐阅读
- list - 在 Haskell 中解决递归问题后出现警告
- file-upload - TYPO3:如何使用文件格式限制 Powermail 中的文件上传
- amazon-cognito - getUserData 在页面刷新或 componentDidMount 时返回旧的 UserData
- redis - Redis Geoadd 没有按照用户存储准确的纬度和经度输入
- filter - 如何使文件选择器对话框返回短路径?
- perl - 如何使用 Perl 以分钟为单位获取日期差异
- java - 如何让 EventSource.onmessage 工作?
- django - django ORM 聚合计数
- excel - VBA用不同的文本替换多个范围
- java - 使用 Java 中的批处理在 Windows Server 中写入 txt 时出现问题