Scala Play 框架模板中的递归块

发布于 2024-12-08 19:54:47 字数 1075 浏览 0 评论 0原文

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

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自在安然 2024-12-15 19:54:47

这对我有用:

将代码括在 @{}

@{

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

   showComment(...)

}
  • 定义递归方法,
  • 在块利润的末尾调用该方法

This works for me:

Enclose code in @{}

@{

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

   showComment(...)

}
  • define recursive method
  • call the method at the end of the block
  • profit !
‖放下 2024-12-15 19:54:47

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

I was able to get past this by moving the recursive template into its own file.

贱人配狗天长地久 2024-12-15 19:54:47

在 Scala 中,递归方法需要返回类型:请参阅 为什么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>
}

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:

@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>
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文