Url params

74 views
Skip to first unread message

maikklein

unread,
Sep 22, 2012, 12:48:04 PM9/22/12
to lif...@googlegroups.com
Hello,

I am trying to do something like this:


I want to render a different page based on the name "xxxxx"

www.test.com/guides/awesomeguide

www
.test.com/guides/newguide

My approach would be I would have a sitemap like this

Menu.i("guides") / "guides"


-----------------------------------------


guides
.html


<div lift:Guides.userGuides> </div>


------------------------------------------------
class Guides{
def userGuides = {
/
/ render different page
}


}

So I would have one snippet which renders me a new page based on the input. I already managed to get the input like this


def howdy() = {


    val s
= Req.parsePath(S.uri).partPath


    println
("+++++++ ")


    s
.foreach(s => println(s))


   
"#time *" #> date.map(_.toString)
 
}

I get the url in a list. If the url is /guides/test I would get List("guides","test")
Then I could query my database with the name and render the html page. 

But I ran into a small problem. Because lift thinks /guides/test is a html file I always get the error "The Requested URL /index/test was not found on this server" . Which is what I was expecting.

I need a way to tell lift "Hey if you see /guides/ the following thing would be a variable so don't try to look for a html file".

In Play2 (MVC) it looked something like this

/guides/:guideName                   controllers.Guides.userGuides(guideName: String)


I am not sure how you call this in lift. I think the solution could be on http://simply.liftweb.net/index-3.4.html#prev 3.4.5 Param Example 
But this is really strange and I couldn't get it to work and I am not quite sure if this is what I want. So for simplicity I just parsed the url into a list.

My best guess would be that it generates somehow a sitemap in "Params" But I don't see the connection between Class ShowParam and object Param. And Param never gets called.

What would you recommend me to look into?





AGYNAMIX Torsten Uhlmann

unread,
Sep 22, 2012, 1:02:43 PM9/22/12
to lif...@googlegroups.com
Maik,

actually the param example, using a LocParam, is the recommended way to do that.

You would make the name of the guide a parameter to the Guides snippet by plumbing in the LocParam to that guides menu location. Lift will then pass the guide name into your Guides snippet where you work on it.

Another (more common, not recommended) way to do it is using "LiftRules.statefulRewrite.append" to append a rule
that basically says: "when encountering /guides/xxx" go to page guides.html and pass in "xxx" as a parameter. That also works, but leaves more of the plumbing to you.

Take a few looks on the param example, then come back and look again. It's not necessarily easy to understand because it's different from what you might be used to, and yet it works and works great.

Hope that helps,

Torsten.

-- 
AGYNAMIX(R). Passionate Software.
Inh. Torsten Uhlmann | Buchenweg 5 | 09380 Thalheim
Phone:       +49 3721 273445
Fax:             +49 3721 273446
Mobile:       +49 151 12412427
Web:           http://www.agynamix.de

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
 
 
 

Ben Phelan

unread,
Sep 22, 2012, 1:16:59 PM9/22/12
to lif...@googlegroups.com
Quick and dirty example with 2 params:

1. Create a param menu
  def someParamChecks(params: CaseClassForParams): Boolean = {
     // do some early param checks
     true
  }

  val someParamMenu = Menu.params[CaseClassForParams](
    "MyPage",
    S ? "MyPage",
    params => 
      if (!someParamChecks(params)) Empty
      else Full(CaseClassForParams(params(0), params(1))),
    params => params.toList
  )

2. Insert into your sitemap
  Menu(someParamMenu              / * / "stuff" / * >> User.loginFirst)


3. Do this in your snippet
  case class CaseClassForParams(firstParam: String, secondParam: String) {
    def toList = firstParam :: secondParam :: Nil
  }

  class SomeSnippet (params: CaseClassForParams) extends DispatchSnippet {
    def dispatch = {
      case "render" => render
    }
  
    def render (in: NodeSeq): NodeSeq = {
      // use the params var here
    }
  }


4. Put your template at src/main/webapp/star/stuff/star.html

Ben Phelan

unread,
Sep 22, 2012, 1:21:56 PM9/22/12
to lif...@googlegroups.com
Actually someParamChecks should take a List[String] ... 

maikklein

unread,
Sep 22, 2012, 1:24:54 PM9/22/12
to lif...@googlegroups.com
Thanks, okay I will experiment a little bit. :)

Peter Petersson

unread,
Sep 22, 2012, 3:35:37 PM9/22/12
to lif...@googlegroups.com
If you want some more param examples, you can take a look at the menu param code at my Templating-With-Twitter-Bootstrap [1] 
There is a sidebar menu to the right on the front page that contains Countries and ParamTest menu entry's.

[1] https://github.com/karma4u101/Templating-With-Twitter-Bootstrap

best regards
  Peter Petersson

On 09/22/2012 07:24 PM, maikklein wrote:
Thanks, okay I will experiment a little bit. :) --

maikklein

unread,
Sep 22, 2012, 4:37:50 PM9/22/12
to lif...@googlegroups.com
This is the first thing that is not quite obvious, but thanks to your examples I am pretty sure I will be able to get used to.

Thank you very much.

Kevin Locke

unread,
Sep 22, 2012, 10:01:36 PM9/22/12
to lif...@googlegroups.com
Sorry to be late, but if you are still struggling you may also find
the wiki page[1] to be helpful.

1. https://www.assembla.com/spaces/liftweb/wiki/Location_Parameters

Kevin

maikklein

unread,
Sep 24, 2012, 12:22:38 PM9/24/12
to lif...@googlegroups.com
thanks for the resources but somehow I permanently run into some issues.


For example, I always get an exception, I uploaded the app to cloudfoundry so that you can see it


I defined a sitemap for /form

Menu.i("Form") / "form",



the form.html looks like this


<div id="main" class="lift:surround?with=default&at=content">
   
<div>
        Params :
<span class="lift:Param">???</span>
   
</div>
</div>



and the snippet

package code
package snippet




import net.liftweb._
import util.Helpers._
import http._
import common._
import sitemap._
case class ParamInfo(theParam: String)


object Param {
 
// Create a menu for /param/somedata
  val menu
= Menu.param[ParamInfo]("Form", "Form",
    s
=> Full(ParamInfo(s)),
    pi
=> pi.theParam) / "form"
  lazy val loc
= menu.toLoc
 
def render = "*" #> (loc.currentValue.map(_.theParam))
}


Any idea why I get this exception?

David Pollak

unread,
Sep 24, 2012, 3:25:52 PM9/24/12
to lif...@googlegroups.com
Please include example code (please see https://www.assembla.com/wiki/show/liftweb/Posting_example_code ) so we can run your code and see what's happening.

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
 
 
 



--
Telegram, Simply Beautiful CMS https://telegr.am
Lift, the simply functional web framework http://liftweb.net


Reply all
Reply to author
Forward
0 new messages