package code.snippet
import net.liftweb.http.S
import scala.xml.Group
import scala.xml.NodeSeq
import net.liftweb.common.Full
import scala.xml.Elem
import scala.xml.UnprefixedAttribute
import scala.xml.Null
class Layout {
def render(xhtml: NodeSeq) = {
if (S.request.map(_.ajax_?) == Full(true)) {
S.skipXmlHeader = true
S.skipDocType = true
/**
* We add a data-gc-version attribute to the root element. This
* attribute is read client-side to send a __Lift_GC message back to
* the server to make sure we don't garbage collect fields associated
* with this page. This page is rendered under a different version than
* the original page itself, so we have to avoid garbage collecting
* this page's version in addition to the base page.
*/
val rendered = xhtml match {
case group: Group =>
group.find {
case e: Elem => true
case _ => false
}.map {
case e: Elem => e % (new UnprefixedAttribute("data-gc-version", S.renderVersion, Null))
} getOrElse group
case other => other
}
S.session.map { liftSession =>
liftSession.processSurroundAndInclude("xhr page", rendered)
} openOr rendered
} else {
val layoutFile =
S.attr("is").map {
file => if (file.startsWith("/")) file.substring(1) else ("layouts-hidden/" + file)} openOr "default"
val insertionPoint = S.attr("at") openOr "content"
<lift:surround with={ layoutFile } at={ insertionPoint }>{ xhtml }</lift:surround>
}
}
}