Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a scala function in a play template

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?

like image 482
myborobudur Avatar asked Apr 23 '13 07:04

myborobudur


People also ask

What is Scala template?

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.

What are controllers in Scala?

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.

Which component is responsible for building play framework?

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.


1 Answers

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
    ""
}
like image 78
Fynn Avatar answered Sep 30 '22 16:09

Fynn