In Play/Scala; I am confused as to why this won't compile:
@(tabNames: Seq[String])
@isActive(idx: Int) = {
@if(idx < 1) {
"active"
} else {
""
}
}
<ul class="nav nav-tabs">
@for((tab, idx) <- tabNames.zipWithIndex) {
@views.html.generic.navLi("tab" + idx.toString, tab, isActive(idx))
}
</ul>
The error reads:
found : play.twirl.api.HtmlFormat.Appendable [error] (which expands to) play.twirl.api.Html [error] required: String [error]
@views.html.generic.navLi("tab" + idx.toString, tab, isActive(idx))
It doesn't recognise the call to isActive
within the call to the template and I have tried multiple variations, e.g. @isActive(idx)
, isActive(@idx)
${isActive(idx)}
(as suggested here), etc. This template generates a navigation bar, passing in tab names and checking to see if the nav li
should be active
(configured by class name/JS).
It just seems that the syntax must be different when calling a function within another template call - I can't get it right.
Documentation makes a distinction between
Note the subtle difference in
@
usage betweenReusable pure code block can have arbitrary return type. In your case, to have
String
as return type, you could write: