Playframework 2.0 define function in View Template

2019-06-27 15:55发布

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?

2条回答
Rolldiameter
2楼-- · 2019-06-27 16:34

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) = {

[...]
查看更多
成全新的幸福
3楼-- · 2019-06-27 16:36

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

查看更多
登录 后发表回答