Built-in variable in the Play 2.x Scala template

How to create a built-in variable in a Play 2.x Scala template? The path from the playback guide is not clear to me:

@defining(user.firstName + " " + user.lastName) { fullName =>
  <div>Hello @fullName</div>
}
+5
source share
3 answers

At first you do not create a variable, and a value means read only.

In your example, you created a value fullNamethat is available inside curly braces.

@defining("Farmor") { fullName =>
  <div>Hello @fullName</div>
}

Hello Farmor will be printed

To determine the value that is available globally in your template, simply enclose everything with your curly braces.

eg.

@defining("Value") { formId =>
  @main("Title") {
    @form(routes.Application.addPost, 'id -> formId) {
      @inputText(name = "content", required = true)
      <input type="submit" value="Create">
    }
  }
}

In this example, you can use the value of formIdanywere.

+10
source

@defining, reusable block, , :

@fullName = @{
  user.firstName + " " + user.lastName
}

<div>Hello @fullName</div>

: https://github.com/playframework/Play20/blob/master/samples/scala/computer-database/app/views/list.scala.html

+9

Easy, put your block with code from the sample, then you can use a variable @fullNamethat matters:

user.firstName + " " + user.lastName
+2
source

All Articles