Updates to sitemap.Loc

42 views
Skip to first unread message

David Pollak

unread,
Oct 13, 2008, 3:15:36 PM10/13/08
to liftweb
Folks,

I'm made a ton of updates to sitemap.Loc.  If you're using sitemap generically, these changes will not impact you.  However, there are a bunch of new features:
  • Loc is type-safe in terms of parameters... they're not just name-value pairs
  • Links and Text for a given Loc can be generated by passing in the appropriate type-safe parameters
  • Re-write rules can be part of Locs and can extract type-safe parameters which can be accessed and used in subsequent calls
  • Locs can have page-specific snippets that take as their parameter the parameter generated by the Loc during URL re-writing
I'll illustrate with code for a page that does all the wiki stuff:

/**
 * The WikiStuff object that provides menu, URL rewriting,
 * and snippet support for the page that displays wiki contents
 */
object WikiStuff extends Loc[WikiLoc] {
  object AllLoc extends WikiLoc("all", false)

  // the name of the page
  def name = "wiki"

  // the default parameters (used for generating the menu listing)
  def defaultParams = Full(WikiLoc("HomePage", false))

  // no extra parameters
  def stuff = Nil

  // is the current page an "edit" or "view"
  def currentEdit = foundParam.is.map(_.edit) openOr false

  /**
   * Check for page-specific snippets and
   * do appropriate dispatching
   */
  override val snippets: SnippetTest = {
    case ("wiki", Full(AllLoc)) => showAll _
    case ("wiki", Full(wp @ WikiLoc(_ , true))) => editRecord(wp.record) _
    case ("wiki", Full(wp @ WikiLoc(_ , false)))
    if !wp.record.saved_? => editRecord(wp.record) _
   
    case ("wiki", Full(wp: WikiLoc)) => displayRecord(wp.record) _
  }


  /**
   * Generate a link based on the current page
   */
  val link =
    new Loc.Link[WikiLoc](List("wiki"), false) {
      override def createLink(in: WikiLoc) = {
    if (in.edit)
      Full(Text("/wiki/edit/"+urlEncode(in.page)))
    else
      Full(Text("/wiki/"+urlEncode(in.page)))
      }
    }

  /**
   * What's the text of the link?
   */
  val text = new Loc.LinkText(calcLinkText _)
 
 
  def calcLinkText(in: WikiLoc): NodeSeq =
    if (in.edit)
      Text("Wiki edit "+in.page)
    else
      Text("Wiki "+in.page)

  /**
   * Rewrite the request and emit the type-safe parameter
   */
  override val rewrite: LocRewrite =
    Full({
      case RewriteRequest(ParsePath("wiki" :: "edit" :: page :: Nil, _, _,_),
              _, _) =>
      (RewriteResponse("wiki" :: Nil), WikiLoc(page, true))

      case RewriteRequest(ParsePath("wiki" :: page :: Nil, _, _,_),
              _, _) =>
      (RewriteResponse("wiki" :: Nil), WikiLoc(page, false))

    })

Questions?

Thanks,

David

--
Lift, the simply functional web framework http://liftweb.net
Collaborative Task Management http://much4.us
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

Tim Perrett

unread,
Oct 13, 2008, 4:32:20 PM10/13/08
to Lift
Awesome stuff David!

>    - Locs can have page-specific snippets that take as their parameter the
>    parameter generated by the Loc during URL re-writing

Out of interest, is this element of implementation useful for the
dynamic form building we discussed some time ago? A snippet for each
step of the form etc

Cheers

Tim

David Pollak

unread,
Oct 13, 2008, 4:36:59 PM10/13/08
to lif...@googlegroups.com

I'll probably roll some of this into the Wizard code.
 


Cheers

Tim

Charles F. Munat

unread,
Oct 13, 2008, 8:56:52 PM10/13/08
to lif...@googlegroups.com
Did git reset then git pull, then ran mvn jetty:run -U in
liftweb/sites/example/ and got this:

[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [scala:compile {execution: default}]
[INFO] suggestion: remove the scalaVersion from pom.xml
[INFO] Compiling 2 source files to
/private/var/www/liftweb/sites/example/target/classes
[WARNING] Exception in thread "main" java.lang.RuntimeException:
malformed Scala signature of WikiEntry at 9961; reference type _6 of
<none> refers to nonexisting symbol.
[WARNING] at
scala.tools.nsc.symtab.classfile.UnPickler$UnPickle.errorBadSignature(UnPickler.scala:755)
...
[INFO]
------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO]
------------------------------------------------------------------------
[INFO] command line returned non-zero value:1

Chas.

David Pollak wrote:
> Folks,
>
> I'm made a ton of updates to sitemap.Loc. If you're using sitemap
> generically, these changes will not impact you. However, there are a
> bunch of new features:
>

> * Loc is type-safe in terms of parameters... they're not just
> name-value pairs
> * Links and Text for a given Loc can be generated by passing in the
> appropriate type-safe parameters
> * Re-write rules can be part of Locs and can extract type-safe


> parameters which can be accessed and used in subsequent calls

> * Locs can have page-specific snippets that take as their parameter

David Pollak

unread,
Oct 13, 2008, 10:57:18 PM10/13/08
to lif...@googlegroups.com
Please always do a "mvn clean" after doing a pull.

Charles F. Munat

unread,
Oct 14, 2008, 12:05:59 AM10/14/08
to lif...@googlegroups.com
I thought I did (I even blew away .m2), but I guess not because after
blowing away the repository it seems to be working. I'm very tired, so I
probably missed it. Sorry for the false alarm.

Chas.

Reply all
Reply to author
Forward
0 new messages