Scala Play 框架模板中的递归块
我正在为一篇博客文章编写一个模板,其中包含评论。为线程注释编写模板的一种自然方式是使用递归方式构建 Html。像这样的事情:
@showComment(comment: models.Comment) = {
<div class="comment">
<div class="comment-metadata">
<span class="comment-author">by @comment.author,</span>
<span class="comment-date">
@comment.postedAt.format("dd MMM yy")
</span>
</div>
<div class="comment-content">
<div class="about">Detail: </div>
@Html(comment.content.replace("\n", "<br>"))
</div>
<a href="@action(controllers.Application.replyComment(comment.id()))">Reply</a>
@comments filter { c => c.parent_id == comment.id } map {
c => @showComment(c)
}
</div>
}
问题是使用递归块会产生错误:
引发的错误是:递归方法 showComment 需要结果类型
如果我尝试将返回类型放入 showComment 中,则会引发此错误:
引发的错误是:未找到:值 showComment
有解决方法吗?
I'm writing a template for a blog post, which has threaded comments. A natural way of writing a template for threaded comments it use a recursive way for constructing the Html. Something like this:
@showComment(comment: models.Comment) = {
<div class="comment">
<div class="comment-metadata">
<span class="comment-author">by @comment.author,</span>
<span class="comment-date">
@comment.postedAt.format("dd MMM yy")
</span>
</div>
<div class="comment-content">
<div class="about">Detail: </div>
@Html(comment.content.replace("\n", "<br>"))
</div>
<a href="@action(controllers.Application.replyComment(comment.id()))">Reply</a>
@comments filter { c => c.parent_id == comment.id } map {
c => @showComment(c)
}
</div>
}
The problem is that using a recursive block yields the error:
Error raised is : recursive method showComment needs result type
If I try to put a return type in the showComment it raises this errror:
Error raised is : not found: value showComment
Any workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对我有用:
将代码括在
@{}
中This works for me:
Enclose code in
@{}
我能够通过将递归模板移动到它自己的文件中来解决这个问题。
I was able to get past this by moving the recursive template into its own file.
在 Scala 中,递归方法需要返回类型:请参阅 为什么Scala 是否需要递归函数的返回类型?
我对 Play 框架了解不多(更像是一无所知),但请尝试:
In Scala, recursive method require a return type: See Why does Scala require a return type for recursive functions?
I don't know much (more like nothing) about the Play Framework but try: