Search code examples
scalaattributesconditional-statementsscalatags

Scalatags conditional attribute


I'm trying to write a wrapper around the select element. So in principle I want to be able to specify that given some boolean multiple I want to append the multiple attribute or not. Below I've given a small example:

select (id := someId, name := someName, if (multiple) "multiple".attr := "")

This obviously won't compile, but it should convey my intent.


Solution

  • You can try:

    val attrList = if (multiple) List("multiple".attr = "") else List.empty
    select (id := someId, name := someName)(attrList:_*)
    

    This way its conditional whether you add that attribute or not.