Declare Variable in a Play2 Scala Template

Declare variable in a Play2 scala template

@defining("foo") { title=>
<div>@title</div>
...
}

basically, you have to wrap the block in which you are going to use it

Play! framework: define a variable in template?

As stated in the Play documentation you can use the @defining helper.

@defining(if (event.getSeverity > 0) "green" else "red") { color =>
<div style="background-color: @color">foo</div>
}

Or you can use a reusable block

@severityColor(event: Event) = @{
if (event.getSeverity > 0) "green" else "red"
}

<div style="background-color: @severityColor(event)">foo</div>

How to declare and access local variables in scala template in play framework?

Actually I have never seen @if nor have I tried PlayFramework. But if is what I think it is, it seems that when you actually try to ask for letter it's already out of scope. What happens if you re-arrange the brackets as follows?

@for(col <- List.range(0,12)) {
<td>
@if(col % 2 == 0) {
@{val letter = someMap(col)
<div class="z@(letter)@(letter)s"></div>
}
}
</td>
}

Variable in Play Framework 2.x Template

Just remember to do it in one line

@import scala.Predef; var menuitems_from_database = menuitems.fetchFromVerySlowDatabase();

An example of the live code (modification of the index.scala.html from the default template):

@import scala.Predef; var menuitems_from_database = new java.util.ArrayList[String]()
@(message: String)

@{
menuitems_from_database.add("1")
menuitems_from_database.add("2")
""
}

@main("Welcome to Play") {
@if(menuitems_from_database != null) {
<ul>
@for(menuitem <- menuitems_from_database) {
<li><a href="@menuitem">@menuitem</a></li>
}
</ul>
}
}

Resulting HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome to Play</title>
<link rel="stylesheet" media="screen" href="/assets/stylesheets/main.css">
<link rel="shortcut icon" type="image/png" href="/assets/images/favicon.png">
<script src="/assets/javascripts/hello.js" type="text/javascript"></script>
</head>
<body>
<ul>
<li><a href="1">1</a></li>
<li><a href="2">2</a></li>
</ul>
</body>
</html>

Dynamic pass value in a Play2 scala template

You should not mix Twirl templating with Javascript. It's a bad approach.
The role for Twirl is to render HTML blocks. You can define conditions and variables here in order to dynamically change the HTML output. While with Javascript you can modify this rendered HTML output without reloading the page.

There are cases where you need to use a Twirl variable in Javascript, then you can do something like:

@(chartData: Html)

<script>
let jsData = @twirlData; // where twirlData is an existing variable
console.log(jsData)
</script>



Here's a link where you can read more.

Initailzing variable in scala template

You can fetch the index of the iteration in Scala for loop, just zipWithIndex your collection:

@for((day, index) <- model.days.zipWithIndex) {
<li>Day @index is @day</li>
}

like described in other question

Call play2 template with variable arguments of type Html

You can use a workaround:

Example call in a template file:

@TabsBuilder{
<a>tab1</a>
}{
<a>tab2</a>
}.map(tabs.apply)

The TabsBuilder:

package views.html

import play.api.templates.Html

class TabsBuilder(templates: Vector[Html]) {
def apply(html: Html) = new TabsBuilder(templates :+ html)
def map(f: Seq[Html] => Html) = f(templates)
}

object TabsBuilder {
def apply(html: Html) = new TabsBuilder(Vector(html))
}

The TabsBuilder enables you to write the code like you would have a variable number of parameter lists.



Related Topics



Leave a reply



Submit