简体   繁体   English

在模板中播放框架字符串格式

[英]play framework string format in template

I am using play framework 1.4.x, In controller , render a List object like this 我正在使用播放框架1.4.x,在controller中,渲染这样的List对象

List<Article> articles = Article.find("Category = ? order by id desc", cateId).fetch();
render(articles)

In views,html template 在视图中,html模板

%{ for (item in articles) { }%
<div>
 ${item.title}
<li>${item.description}</li>
</div>
%{ } }%

but, the description is to long, need to do some kind of string format,or cut work,then show in the template. 但是,描述太长了,需要做某种字符串格式,或者剪裁一下,然后在模板中显示。 I have already google for the template syntax but got nothing. 我已经用谷歌搜索模板语法,但一无所获。

thanks for @Md Ayub Ali Sarker. 感谢@Md Ayub Ali Sarker。 but I write a pure scala function in template , It throws a template compilation error. 但是我在template中编写了一个纯scala函数,它引发了模板编译错误。

抛出模板编译错误

It has fixed. 它已修复。 1.you can define a function in somewhere like utils. 1.您可以在utils之类的地方定义一个函数。 2.in the views template,you can use these code call the utils function 2.在视图模板中,可以使用这些代码调用utils函数

%{hasBeenCuted = utils.Cut.cut(oginalKeywordsNeedToCut)}%
${hasBeenCuted}

write template function in pure Scala code like 用纯Scala代码编写模板函数,例如

 @shortdescription(text: String) = @{
  text.take(10); // take first 10 character // more scala string function http://alvinalexander.com/scala/scala-string-examples-collection-cheat-sheet
}

in view change like this 鉴于这样的变化

%{ for (item in articles) { }%
<div>
 ${item.title}
<li>@shortdescription(${item.description})</li>
</div>
%{ } }%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM