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
Retrieve Image from Ftp to Webpage
How to Make a Transparent Canvas in HTML5
Force Browsers to Load CSS Before Showing the Page
How to Make a Line Before and After My H1 Tag
Html:How to Show Images in a Textarea
Upload a Whole Directory Through an HTML Form
Min-Width and Max-Height for Table Attributes
How to Get a Specific Number Child Using CSS
Make Row Stretch Across All Columns in CSS Grid
Mailto Link Multiple Body Lines
How to Maintain Scroll Position in MVC
Css3 Scale - Prevent from Scalling Text
Why Are Dashes Preferred for CSS Selectors/HTML Attributes
How to Center the Contents of an HTML Table
Is the HTML Shown via 'View Source' Different from the HTML Shown in (Firebug) Developer Tools
HTML <Base> Tag and Local Folder Path with Internet Explorer