加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala Play Framework模板中的递归块

发布时间:2020-12-16 18:51:20 所属栏目:安全 来源:网络整理
导读:我正在为博客文章编写一个模板,该文章有线程评论.一种为线程注释编写模板的自然方法,它使用递归方式构造 Html.像这样的东西: @showComment(comment: models.Comment) = { div class="comment" div class="comment-metadata" span class="comment-author"by
我正在为博客文章编写一个模板,该文章有线程评论.一种为线程注释编写模板的自然方法,它使用递归方式构造 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>
}

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

Error raised is : recursive method showComment needs result type

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

Error raised is : not found: value showComment

任何解决方法?

解决方法

这对我有用:

将代码括在@ {}中

@{

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

   showComment(...)

}

>定义递归方法>在块的末尾调用方法>利润!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读