Playframework 2.0 define function in View Template

萝らか妹 提交于 2020-01-03 17:29:18

问题


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?


回答1:


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

[...]



回答2:


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



来源:https://stackoverflow.com/questions/10467101/playframework-2-0-define-function-in-view-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!