Up: Chapter 8

8.4 HtmlProperties, XHTML and HTML5

Lift unifies many aspects of parsing and displaying the HTML page in a single trait, HtmlProperties.
HtmlProperties defines, on a session-by-session (and even a request-by-request) basis, the way that templates are parsed and the way that Scala’s NodeSeq is converted into valid HTML output. The properties on HtmlProperties are:

8.4.1 XHTML via OldHtmlProperties

The default properties that keep compability with the disparate LiftRules used to calculate DocType and Encoding. Uses the PCDataXmlParser parser which requires well-formed XML files. Output is generally XHTML via AltXML.toXML, but cerain tags (e.g., <br>) are written in IE6/IE7 friendly ways.

8.4.2 HTML5 via Html5Properties

Prior to Lift 2.2, Lift always emitted XHTML and by default set the Content-Type header to application/xhtml+xml; charset=utf-8. This continues to be Lift’s default behavior. It turns out that most browsers, even modern ones (Firefox, Chrome and Safari) had issues with XHTML. Further, XHTML limited the behavior of certain JavaScript libraries.
By invoking LiftRules.htmlProperties.default.set((r: Req) => new Html5Properties(r.userAgent)) in Boot.scala, you can set Lift to full HTML5 support. Lift uses the nu.validator HTML parser and emits the correct DocType and response headers such that all tested browsers (IE6+, Firefox 2+, Safari 2+, Chrome 1+) render pages correctly.
Because the HTML5 parser is different from the standard XML parser, you will need to adjust your existing templates in the following ways:

8.4.3 Changing behavior mid-session or mid-request

You can change the behavior of HtmlProperties mid-session or mid-request. LiftSession.sessionHtmlProperties is a SessionVar that contains the HtmlProperties for the session. LiftSession.requestHtmlProperties is a TranientRequestVar containing the HtmlProperties for the request. At the begining of the request, requestHtmlProperties is set to the value of sessionHtmlProperties. You can alter a property for the duration of the request using:
for {
  session <- S.session
} session.requestHtmlProperties.set(session.
          requestHtmlProperties.is.setDocType(() => 
                                   Full("<!DOCTYPE moose>")))
Up: Chapter 8

(C) 2012 David Pollak