Scala Play Framework模板中的递归块

Fel*_*mel 6 scala threaded-comments playframework

我正在为博客文章编写一个模板,该文章有线程评论.一种为线程注释编写模板的自然方法,它使用递归方式构造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>
}
Run Code Online (Sandbox Code Playgroud)

问题是使用递归块会产生错误:

引发的错误是:递归方法showComment需要结果类型

如果我尝试在showComment中放置一个返回类型,它会引发这个错误:

引发的错误是:找不到:值showComment

任何解决方法?

小智 4

这对我有用:

将代码附在@{}

@{

    //use regular scala here:
    def showComment(comment: models.Comment):Node = {
    ....
    }
    //the above just declared a recursive method, now call it:

   showComment(...)

}
Run Code Online (Sandbox Code Playgroud)
  • 定义递归方法
  • 调用块末尾的方法
  • 利润 !