I try to use a function in a play view template
@active(path: String):String = @{
var active:String = ""
if (request.path.startsWith(path)) {
active = "class=\"active\""
}
return active
}
<div class="container-fluid">
....
<li @active("/page") ...>
The play compiler says that it can't find the value active. What's wrong here?
A Play Scala template is a simple text file that contains small blocks of Scala code. Templates can generate any text-based format, such as HTML, XML or CSV. The template system has been designed to feel comfortable to those used to working with HTML, allowing front-end developers to easily work with the templates.
A Controller is a Scala singleton object, hosted by the controllers package, and subclassing play. mvc. Controller . In Scala you can declare as many controllers you want in the same file.
Play Framework is an open-source web application framework which follows the model–view–controller (MVC) architectural pattern. It is written in Scala and usable from other programming languages that are compiled to JVM bytecode, e.g. Java.
Try removing the return type of the function and move it to the top of your template. This works in my template (see also: Playframework 2.0 define function in View Template):
@active(path: String) = @{
if (request.path.startsWith(path))
"class=\"active\""
else
""
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With