Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline variable in the Play framework 2.x Scala template

How to create an inline variable in the Play framework 2.x Scala template? Path from the Play's guide is not clear to me:

@defining(user.firstName + " " + user.lastName) { fullName =>
  <div>Hello @fullName</div>
}
like image 840
user1051870 Avatar asked May 06 '12 18:05

user1051870


2 Answers

First you don't create a variable but a value meaning it's read only.

In your example you have created a value fullName which is accessible inside the curly brackets.

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

Will print Hello Farmor

To define a value which is accessible globally in your template just embrace everything with your curly brackets.

E.g.

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

In the example you can use the value formId anywere.

like image 181
Farmor Avatar answered Oct 21 '22 03:10

Farmor


If you don't want to use the @defining syntax you can define a reusable block which will be evaluated every time you use it:

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

<div>Hello @fullName</div>

With this same syntax you can also pass arguments to the block: https://github.com/playframework/Play20/blob/master/samples/scala/computer-database/app/views/list.scala.html

like image 44
OlivierBlanvillain Avatar answered Oct 21 '22 01:10

OlivierBlanvillain