Nasir
Nasir

Reputation: 2984

Custom tag in playframework2.0 or perform equals in a template

I need to perform an equals comparison in my template, inorder to do a conditional render.

For instance, I have <li class="active">. In here, I would like active only to be rendered if @request.path and @controllers.routes.Application.action() are equal, otherwise a blank string is fine.

the if statement as described in docs appears to be limited in recognizing the paranthesis and hence does not work.

Alternatively if you could help me with creating a custom tag, that could also work. There is no documentation on how to do that. I'll prefer java. If there is a scala alternative, please err on the side of verbosity as I have yet to figure it out.

Thanks.

Upvotes: 0

Views: 236

Answers (1)

Maaaaat
Maaaaat

Reputation: 336

Without a custom tag, you can try something like this in your template :

<li class="@("active".when(request.path == controllers.routes.Application.action().toString()))">

There is an example in samples applications (samples/java/forms/app/views/main.scala.html).
Dont forget the toString() because action() returns a Call, not a String).

A custom tag to do that could be :

@menu_element(name: String, route: Call) = {
 <li class="@("active".when(route.toString() == request.path))"><a href="@route">@name</a></li>
}

Example of use :

<ul>
 @menu_element("Home", controllers.routes.Application.index)
</ul>

Upvotes: 1

Related Questions