sergeda
sergeda

Reputation: 2201

How to use variable in reusable block in Play Framework 2 (Scala)

I can't find the way how to interpolate variable in reusable block. I've tried this without luck:

@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)
    }
}

Upvotes: 0

Views: 177

Answers (1)

Peanut
Peanut

Reputation: 3963

You should pass your variables as parameter:

@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)
    }
}

Note: I guessed what type user and site had. You need to changes those of course.

Have a look at the documentation: https://www.playframework.com/documentation/2.4.x/ScalaTemplates#Declaring-reusable-blocks

Upvotes: 1

Related Questions