Is there an invisible-parent tag collection object available?

84 views
Skip to first unread message

X

unread,
Dec 30, 2014, 8:12:46 AM12/30/14
to scal...@googlegroups.com
Is there something like an invisible tag container object/collection?

I'm looking for this for the following problem: how to render multiple elements that are under a condition?
Example, I would like to output:
<div>
 
<span>info</span>
  ,
<span>delete</span>
</div>

where this part is optional:
,<span>delete</span>
(only moderators can see ", delete")

How can I code this in Scalatags? The following doesn't work:
div(
 
span("info"),
  if (isModerator) ", " + span("delete") else ""
)

because this causes the if-body to be seen as a String (instead of a Scalatags structure) and thus be escaped by ScalaTags.

The only solution that I currently have is:
div(
  span
("info"),
 
if (isModerator) raw(" ," + span("delete").toString) else ""
)
But the "raw" and "toString" part feel a bit uncomfortable.

Is there something like this?:
div(
  span
("info"),
 
if (isModerator) List(" ,", span("delete")) else ""
)
that would be cleaner in my opinion.

X

unread,
Dec 30, 2014, 8:47:00 AM12/30/14
to scal...@googlegroups.com
This does work, but a bit verbose with the type parameter "Modifier"
div(
  span("info"),
  if (isModerator) List[Modifier](" ,", span("delete")) else ""
)

But couldn't get the code compiling yet using something like this:
object STList {
  def apply(elements: Modifier*): List[Modifier] = {
    List[Modifier](elements: _*)
  }
}

div(
  span("info"),
  if (isModerator) STList[Modifier](" ,", span("delete")) else ""
)




Op dinsdag 30 december 2014 14:12:46 UTC+1 schreef X:

X

unread,
Dec 30, 2014, 9:21:13 AM12/30/14
to scal...@googlegroups.com
I was using the wrong Modifier in the previous solution. This one works:

import scalatags.Text.all.Modifier

object STList {
  def apply(elements: Modifier*): List[Modifier] = {
    List[Modifier](elements: _*)
  }
}


div(
  span("info"),
  if (isModerator) STList(" ,", span("delete")) else ""
)

Is this the preferred way to use Scalatags?


Op dinsdag 30 december 2014 14:12:46 UTC+1 schreef X:
Is there something like an invisible tag container object/collection?

re...@reactific.com

unread,
Dec 30, 2014, 9:39:04 AM12/30/14
to scal...@googlegroups.com
What works for me is a construct like:

if (condition) Seq(modifiers) else Seq.empty[Modifier]

That is actually so commonplace that we might want to make something like this

object conditional {
  def apply(cond: => Boolean, value: Modifier*) {
    if (cond) value.toSeq else Seq.empty[Modifier]
  }
}

I imagine this could be used like:

div("span("info"), conditional(isModerator, ",", span("delete")))

Would something like that work?


Please note: I haven't compiled the above, just wrote it off the top of my head so please forgive any typos/errors.

Reid.

Haoyi Li

unread,
Dec 30, 2014, 10:13:31 AM12/30/14
to Reid Spencer, scal...@googlegroups.com
In the latest master () is now an invisible marker; otherwise Seq.empty or None or "" work too

--
You received this message because you are subscribed to the Google Groups "Scalatags" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalatags+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

X

unread,
Dec 30, 2014, 10:28:14 AM12/30/14
to scal...@googlegroups.com
What works for me is a construct like:

if (condition) Seq(modifiers) else Seq.empty[Modifier]
I'm getting a compile error when using Seq without type parameters:
  div(
    span("info"),
    if (condition) Seq(", ", span("delete")) else ""
  )

error:
type mismatch;
[error]  found   : Seq[java.io.Serializable]
[error]  required: scalatags.Text.Modifier
[error]     (which expands to)  scalatags.generic.Modifier[scalatags.text.Builder]
[error]           if (isAdmin) Seq(", ", a(href:=psetEditLink)("edit")) else ""

although is does work with type parameter (Seq[Modifier](.....)), but that's similar to my List[Modifier] solution, which both are a bit verbose because of the type parameter. The STList solution does work without type parameter. 

The conditional object solution works fine (") {" has to be changed to ") = {").

X

unread,
Dec 30, 2014, 10:41:50 AM12/30/14
to scal...@googlegroups.com
Seq(modifiers)
doesn't seem to work. I have to add a type parameter:
Seq[Modifier](modifiers)
But it would be better to shield users from ScalaTag internals such as "Modifier", right?

Although the "conditional" object solution does work. (Note that ") {" has to be changed to ") = {".)
But I think there are still situations in which even without conditionals, there's a need for a grouping/collection element like Seq but without type parameters.

Haoyi Li

unread,
Dec 30, 2014, 10:51:33 AM12/30/14
to X, scal...@googlegroups.com
I don't consider Modifier to be internal; I'd say Modifier and Frag are user-facing types. Maybe we could figure out a way to make the type inference work better though. I don't fully grok the intricacies of type inference in scalac, as is evidenced by issues like https://github.com/lihaoyi/scalatags/issues/16

Byron Weber Becker

unread,
Jan 27, 2015, 3:28:41 PM1/27/15
to scal...@googlegroups.com
+1 on this request.  For another usage you need look no further than the ScalaTags documentation.  We all know that builtin tags can take a sequence of tags as children.  For example,

```div(p("one"), p("two"))```

However, if a user wants to write a function that does the same we need to write (see the documentation under "Layout")

```def page(scripts: Seq[Frag], content: Seq[Frag]) =...```

and then explicitly wrap the fragments as a sequence.  It would be really nice to be able to avoid that Sequence -- the the mental overhead of knowing when you need to wrap things as a sequence and when you don't.

The problem I'm banging my head on right now is similar page, except that I expect its usage to be much more frequent.  Therefore, I'm trying to make it "nice" but feel like I keep getting hung up on passing a sequence of tags to it.

Byron 
Reply all
Reply to author
Forward
0 new messages