Creating HTML comments and conditional comments with Scalatags

179 views
Skip to first unread message

Mark Sivill

unread,
Mar 13, 2015, 7:47:15 AM3/13/15
to scal...@googlegroups.com
Hi,

I trying to build Twitter bootstrap on the Play Framework using scalatags instead of the default Twirl template ( http://getbootstrap.com/getting-started/ )

It requires I generate the following conditional comment within the HTML -

<!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

I haven't been able to find a tag to do this within scalatags, and I tried to put the raw string into the Text.Modifier however
it gets HTML escaped therefore does not become a comment and is displayed on the screen.

The text gets escaped....

body(
h1("mock up")
,"<!-- this is a comment test -->"
)

What is the correct way to get comments and conditional comments into the HTML using scalatags.

Thanks

Mark



Mark Sivill

unread,
Mar 16, 2015, 6:30:19 AM3/16/15
to scal...@googlegroups.com
So by using the scalatags raw() method you can put comments and conditional comments into the HTML.

I created the following utility method to aid reuse...

  def comment(comment:String) = raw("<!-- " + scalatags.Escaping.escape(comment, new StringBuilder) + " -->")

  def conditionalComment(condition:String, xs:Text.Modifier *) = Seq(raw("<!--[if " + scalatags.Escaping.escape(condition,new StringBuilder) + "]>")) ++ xs.toSeq :+ raw("<![endif]-->")


Mark Sivill

Haoyi Li

unread,
Mar 16, 2015, 8:27:01 AM3/16/15
to Mark Sivill, scal...@googlegroups.com
Seems reasonable. I doubt we're gonna have in-built support, since `raw` seems easy enough ^_^

--
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.

Reply all
Reply to author
Forward
0 new messages