Wei Ma Wei Ma - 9 months ago 80
Scala Question

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)

<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?

Answer Source

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