Playframework 2.0在视图模板中定义功能 [英] Playframework 2.0 define function in View Template

查看:92
本文介绍了Playframework 2.0在视图模板中定义功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PlayFramework 2.0开发一个项目.阅读了一些scala之后,我想在View模板中嵌入一些动态代码.因此,我做了以下事情:

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

我非常确定它会起作用,但是相反,我得到了未找到:值getMyString错误发生"的信息.我做错了什么吗?

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?

推荐答案

问题在于播放定义了一个非常狭窄的范围,无法识别其当前大括号之外的def.

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

您可以更改def的最后一个花括号的位置,使其包含输入标签,然后它应该可以工作.

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

[...]

这篇关于Playframework 2.0在视图模板中定义功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆