GSPで定義タグって使えるの?(Grails 0.5+オリジナルパッチ)
こんな感じのGSPを作ってみた。
<%! def square = { x -> x * x } %> <html> <head> <title>Test</title> </head> <body> 3 ^ 2 = <%= square(3) %> </body> </html>
これ、コンパイルできないみたい。Parse.javaとかみると、<%! %>の処理はあるみたいなんだけど..未実装?それともGSPでは使えない?それともバグ?
ParseTest.javaに以下のようなテストを追加して実行してみた。
public void testParseWithDeclare() throws Exception { String output = parseCode("myTest", "<%! def hoge = 'hoge' %>" + "<div>Hello</div>"); System.out.println(output); }
結果として表示されたのは、
import org.codehaus.groovy.grails.web.pages.GroovyPage import org.codehaus.groovy.grails.web.taglib.* def hoge = 'hoge' class myTest extends GroovyPage { public Object run() { out.print('<div>Hello</div>') } }
だった。def hogeの文がmyTestクラスの中にあるのが正しいのかな。