Up: Chapter 13

13.4 Creating the Snippet

Next you have to tell Lift what the rules are for transforming the section of your template based on dynamic rules. This is a Snippet... it’s a function that transforms NodeSeq => NodeSeq. Let’s look at the TimeNow snippet:
TimeNow.scala
// make sure this is the snippet package so Lift
// can find the snippet
package code
package snippet
​
// some inputs
import net.liftweb._
import util._
import Helpers._
​
// our snippet
object TimeNow {
  // create a function (NodeSeq => NodeSeq)
  // that puts the current time into the
  // body of the incoming Elem
  def render = "* *" #> now.toString
}
This snippet must be in the snippet package so Lift knows how to find it by convention.
It is an object which is a singleton because the snippet has no state.
Lift calls the render method on a snippet unless you specify another method when you invoke your snippet.
The snippet generates a function, NodeSeq => NodeSeq, that uses Lift’s CSS Selector Transforms (See 7.10 on page 1↑) to insert the current time into the body of all HTML Elements: def render = "* *" #> now.toString
Up: Chapter 13

(C) 2012 David Pollak