Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playframework 2.0 define function in View Template

I am working on a project using PlayFramework 2.0. After reading a bit of scala I would like to embed some dynamic code in the View template. So, I did the following:

@{
    def getMystring(sequence:Int) = {
        if(patternForm != null && 
            patternForm.get().windowTreatments != null &&
            patternForm.get().windowTreatments.size() >= sequence + 1)
            sequence+""
        else 
            "" 
    }
}

<input type = "text" value = @getMystring(1)></input>
...

I was quite sure it was going to work but instead I got a "not found: value getMyString Error occurred" . Did I do something obviously wrong?

like image 637
Wei Ma Avatar asked Feb 20 '23 14:02

Wei Ma


2 Answers

try starting it like a template, like this

@getMystring(sequence:Int) = {

[...]

have a look at https://github.com/playframework/Play20/blob/master/samples/scala/computer-database/app/views/list.scala.html

like image 68
opensas Avatar answered Mar 03 '23 12:03

opensas


The problem being that play defines a very narrow scope and can't recognize defs outside its current curly brackets.

You can change the position of the last curly bracket for your def to include the input tag and then it should work.

Or you can do what opensas suggested.

@getMystring(sequence:Int) = {

[...]
like image 37
Farmor Avatar answered Mar 03 '23 10:03

Farmor