如何在Play Framework 2(Scala)中的可重用块中使用变量

时间:2015-07-27 13:44:26

标签: scala playframework-2.0

我找不到如何在可重用块中插入变量的方法。 我没试过就试过这个:

@headers = @{
    page match {
        case "home" => Map(
                "title" -> "Welcome",
                "description" -> "Welcome to our site")
        case "profile" => Map(
                "title" -> "@user.name - @site.name",
                "description" -> "Hello @user.name")
        }
    }


@headers = @{
    page match {
        case "home" => Map(
            "title" -> "Welcome",
            "description" -> "Welcome to our site")
        case "profile" => Map(
            "title" -> user.name + "-" site.name,
            "description" -> "Hello" + user.name)
    }
}

1 个答案:

答案 0 :(得分:1)

您应该将变量作为参数传递:

@headers(user:User,site:Site) = @{
    page match {
        case "home" => Map(
            "title" -> "Welcome",
            "description" -> "Welcome to our site")
        case "profile" => Map(
            "title" -> user.name + "-" site.name,
            "description" -> "Hello" + user.name)
    }
}

注意:我猜到了usersite的类型。当然,你需要改变它们。

查看文档:{​​{3}}