6

我正在为博客文章编写模板,其中包含线程评论。为线程注释编写模板的一种自然方式是使用递归方式构建 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

任何解决方法?

4

3 回答 3

4

这对我有用:

附上代码@{}

@{

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

   showComment(...)

}
  • 定义递归方法
  • 在块的末尾调用方法
  • 利润 !
于 2012-12-08T15:06:29.517 回答
2

我能够通过将递归模板移动到它自己的文件中来解决这个问题。

于 2015-02-03T03:47:39.387 回答
0

在 Scala 中,递归方法需要返回类型:请参阅为什么 Scala 需要递归函数的返回类型?

我对 Play Framework 了解不多(更像一无所知),但请尝试:

@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>
}
于 2011-10-10T00:13:06.763 回答