Play框架的模板引擎中是否存在“instanceof”功能?(Is there an “instanceof” functionality in the template engine of the Play framework?)
我想知道Play(2.0)框架的模板引擎中是否存在某种instanceof功能。 这将允许我有效地使用模型层中的继承结构。
这基本上是我想要的(除了这个模板不能编译):
@(instance: Superclass) @main { @if(instance instanceof Subclass) { Subclass instanceOfSubclass = (Subclass) instance; } }编辑:我的意思是在特定的Play for Java中。
I would like to know if there is some kind of instanceof functionality in the template engine of the Play (2.0) framework. This would allow me to effectively use the inheritance structure in my model layer.
This is basically what I would like (except that this template doesn't compile):
@(instance: Superclass) @main { @if(instance instanceof Subclass) { Subclass instanceOfSubclass = (Subclass) instance; } }Edit: I mean in specific Play for Java.
最满意答案
使用Scala模式匹配。
@(instance: Superclass) @main { @instance match { case foo: Foo => { <a href="@routes.Foo.index()">@foo.title</a> } case bar: Bar => { <a href="@routes.Bar.index()">@bar.title</a> } } }Use Scala pattern matching.
@(instance: Superclass) @main { @instance match { case foo: Foo => { <a href="@routes.Foo.index()">@foo.title</a> } case bar: Bar => { <a href="@routes.Bar.index()">@bar.title</a> } } }更多推荐
发布评论