How to express empty node?

428 views
Skip to first unread message

Ngoc Dao

unread,
Oct 6, 2014, 5:39:51 PM10/6/14
to scal...@googlegroups.com
Hi,

I want to write logic like this:

if (condition)
  div("Something")
else
  emptyNode

By "emptyNode", I mean nothing should be output in the resulting HTML.
How to express that?

If possible, please also add the solution to the README at GitHub. It may be helpful for other people too.

Benjamin Jackman

unread,
Oct 8, 2014, 8:26:54 PM10/8/14
to scal...@googlegroups.com
I might be wrong, but think option works aka
if (condition) Some(div("Something")) else None

Ngoc Dao

unread,
Oct 9, 2014, 11:30:44 PM10/9/14
to scal...@googlegroups.com
I've tried with scalatags 0.4.1:

scala> import scalatags.Text.all._
import scalatags.Text.all._

scala> html(div("Something"))
res0: scalatags.Text.TypedTag[String] = <html><div>Something</div></html>

scala> html(if (true) Some(div("Something")) else None)
<console>:11: error: No implicit view available from A => scalatags.generic.Modifier[scalatags.text.Builder].
              html(if (true) Some(div("Something")) else None)

Ngoc Dao

unread,
Oct 9, 2014, 11:34:19 PM10/9/14
to scal...@googlegroups.com
It works if I do like this:

scala> def f(condition: Boolean) = if (condition) Some(div("Something")) else None
f: (condition: Boolean)Option[scalatags.Text.TypedTag[String]]

scala> html(f(true))
res5: scalatags.Text.TypedTag[String] = <html><div>Something</div></html>

scala> html(f(false))
res6: scalatags.Text.TypedTag[String] = <html></html>

Ngoc Dao

unread,
Oct 9, 2014, 11:36:38 PM10/9/14
to scal...@googlegroups.com
It doesn't work if I do like this:

scala> def g(condition: Boolean) = html(if (condition) Some(div("Something")) else None)

<console>:11: error: No implicit view available from A => scalatags.generic.Modifier[scalatags.text.Builder].
       def g(condition: Boolean) = html(if (condition) Some(div("Something")) else None)

Haoyi Li

unread,
Oct 13, 2014, 2:52:44 PM10/13/14
to scal...@googlegroups.com
Yes, using Some and None is possible, it's also very ugly =/ I have yet to figure out a better way of doing this that plays nicely with Scala's type inference. Something like ScalaZ's Boolean.option would be pretty elegant here, and it may be worth defining your own.

Ngoc Dao

unread,
Oct 16, 2014, 4:48:16 PM10/16/14
to scal...@googlegroups.com
It seems that the simple solution is just use empty string to express the empty node:

Stewart Stewart

unread,
Feb 11, 2015, 9:59:58 PM2/11/15
to scal...@googlegroups.com
I'm a bit late to this party, but I've been using the following:

div(if (condition) div else ())

It typechecks and works as expected, ie., it returns either an empty div or a nested div. Please not that the infered type of the `if` expression is `Any` if this isn't written where a `Modifier` is expected.

Landlocked Surfer

unread,
Jan 15, 2016, 4:49:10 AM1/15/16
to Scalatags
Thanks for the tip Steward. That's a solution I find very elegant.
Just wanted to share that this works nicely with attributes also:

val disabled = false
val emptyAttr
= ()

input
(
 
`type`:="checkbox",
 
if(disabled) { "disabled".attr:="" } else { emptyAttr }
)


Scalatags rocks!

Reply all
Reply to author
Forward
0 new messages