如何在模板中处理字符串格式化。

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

How to approach string formatting in templates

问题

我将一个结构体传递给模板,有时其中的字符串长度超过了显示的限制。在其他语言中,我通常会在模板中添加格式化规则。在模板中实现格式化的惯用方法是什么?

示例:

type MyStruct struct {
    something    string
    anotherThing string
}

在模板中

<table>
{{ range .Rows }}      //注意!Rows 是一个 MyStruct 对象的数组
<tr>
<td>{{ .something }}</td>
<td>{{ .anotherThing }}</td>		
</tr>
{{ end }}
</table>

从上面的示例中,问题是:“你会如何确保 .anotherThing 或 .something 的显示不超过40个字符?”

一种解决方案可能是使结构体包含四个值,即两个原始字符串和两个格式化版本,即在 .go 文件中进行格式化,然后始终在悬停时显示原始字符串的工具提示或类似的内容。

英文:

I pass a struct to a template that sometimes contain strings that are a tad too long for display. In any other language, I would've just attached a formatting rule in the template itself. What's the idiomatic approach to accomplish formatting in templates?

Example:

type struct MyStruct{
    something    string
    anotherThing string
}

In the template

&lt;table&gt;
{{ range .Rows }}      //NOTE! Rows is an array of MyStruct objects 
&lt;tr&gt;
&lt;td&gt;{{ .something }}&lt;/td&gt;
&lt;td&gt;{{ .anotherThing }}&lt;/td&gt;		
&lt;/tr&gt;
{{ end }}
&lt;/table&gt;

In case it isn't obvious from the above, the question is "How would you go about making sure .anotherThing or .something doesn't display more than say 40 characters?

One solution COULD be to make the struct contain four values, the two raw strings and two formatted version of them i.e. make the formatting in the .go-file and then always display the raw string in a tooltip on hover or something like that.

答案1

得分: 7

你可以向FuncMap中添加一个自定义的截断函数。有人在playground上发布了一个示例,将模板变量转换为大写,如下所示:

{{ .Name | ToUpper  }}

编辑。将上述代码调整为基本的Truncate过滤器:http://play.golang.org/p/e0eaf-fyrH

{{ .Name | Truncate  }}

如果你想向Truncate传递一个参数,也可以这样写:

{{ Truncate .Name 3 }}

另请参阅:http://play.golang.org/p/Gh3JY1wzcF

英文:

You could add a custom truncate function to the FuncMap. Someone posted an example on the playground, which converts template variables to uppercase, like this:

{{ .Name | ToUpper  }}

Edit. Adjusted above code as a basic Truncate filter: http://play.golang.org/p/e0eaf-fyrH

{{ .Name | Truncate  }}

If you want to pass a parameter to Truncate, you'll can also write it like this:

{{ Truncate .Name 3 }}

See also: http://play.golang.org/p/Gh3JY1wzcF

答案2

得分: 1

除了使用自定义函数之外,还可以通过定义自己的类型并实现Stringer接口来实现另一种方法:

type ShortString string

func (ss ShortString) String() string {
    if len(ss) > 10 {
        return string(ss[:5]) + "..."
    }
    return string(ss)
}

type MyStruct struct {
    Something ShortString
}

playground 上展示了这两种方法。

英文:

One approach besides using a custom function is defining your own type with the Stringer interface:

type ShortString string

func(ss ShortString) String() string {
	if len(ss) &gt; 10 {
		return string(ss[:5]) + &quot;...&quot;
	}
	return string(ss)
}

type MyStruct struct {
	Something    ShortString
}

<kbd>playground</kbd> displaying both approaches.

huangapple
  • 本文由 发表于 2014年8月6日 23:28:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/25164392.html
匿名

发表评论

匿名网友

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

确定