Scala Play框架模板中的递归块
我正在为一篇博客文章编写一个模板,其中包含多线程评论。为线程注释编写模板的一种自然方式它使用递归方式来构造Html。大概是这样的:Scala Play框架模板中的递归块,scala,playframework,threaded-comments,Scala,Playframework,Threaded Comments,我正在为一篇博客文章编写一个模板,其中包含多线程评论。为线程注释编写模板的一种自然方式它使用递归方式来构造Html。大概是这样的: @showComment(comment: models.Comment) = { <div class="comment"> <div class="comment-metadata"> <span class="comment-author">by @comment.author
@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>
}
@showcoment(注释:models.comment)={
作者@comment.author,
@注释.postedAt.格式(“dd-MMM-yy”)
详情:
@Html(comment.content.replace(“\n”和“
”)
@注释过滤器{c=>c.parent_id==comment.id}map{
c=>@showcoment(c)
}
}
问题是使用递归块会产生错误:
引发的错误是:递归方法showComment需要结果类型
如果我尝试在showComment中放置返回类型,则会引发以下错误:
引发的错误为:未找到:值showcoment
任何解决方法?在Scala中,递归方法需要返回类型:请参阅 我对Play框架不太了解(更像是一无所知),但请尝试:
@showComment(comment: models.Comment):Node = {
<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>
}
@showcoment(注释:models.comment):节点={
作者@comment.author,
@注释.postedAt.格式(“dd-MMM-yy”)
详情:
@Html(comment.content.replace(“\n”和“
”)
@注释过滤器{c=>c.parent_id==comment.id}map{
c=>@showcoment(c)
}
}
这对我很有用:
在@{}
@{
//use regular scala here:
def showComment(comment: models.Comment):Node = {
....
}
//the above just declared a recursive method, now call it:
showComment(...)
}
- 定义递归方法
- 调用块末尾的方法
- 利润李>