简体   繁体   中英

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?

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
    ""
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM