Skip to content Skip to sidebar Skip to footer

How To Get Parameters In An HTML Page Passed From A Snippet With Lift Framework

I want to develop a Lift web app, I've an index page that has in here body:
App

Solution 1:

To do what you are looking to do, you would just point the form directly at where ever hello.html is mounted. I am assuming it is hello in the same path.

dumbform.html

<div id="main" class="lift:surround?with=default&at=content">
  <div> App </div> 
  <div>
    <form method="post" action="hello">
       <table>
            <tr><td> Email:</td> <td><input name="email" type="text"></td></tr>
            <tr><td> Password:</td> <td><input name="pwd" type="password"></td></tr>
            <tr><td> Name:</td> <td><input name="name" type="text"></td></tr>
            <tr><td><input type="submit" value="Sign in"></td> <td><input type="reset" value="Reset"></td></tr>
        </table>
    </form>
  </div> 
</div>

hello.html

<div data-lift="ShowHelloSnippet">
  <p>Hello <span name="paramname"></span></p>
</div>

Snippet

class ShowHelloSnippet {
  def render = {
    "@paramname" #> S.param("name")
  }
}

The more Lift way to do it thought, would be to use Lift's SHtml form elements:

dumbform.html

<div id="main" class="lift:surround?with=default&at=content">
  <div> App </div> 
  <div>
    <form method="post" data-lift="FormHandlerSnippet">
       <table>
            <tr><td> Email:</td> <td><input name="email" type="text"></td></tr>
            <tr><td> Password:</td> <td><input name="pwd" type="password"></td></tr>
            <tr><td> Name:</td> <td><input name="name" type="text"></td></tr>
            <tr><td><input id="submitbutton" type="submit" value="Sign in"></td> <td><input type="reset" value="Reset"></td></tr>
        </table>
    </form>
  </div> 
</div>

Snippet

class MyFormResponse(
    var email:String="", 
    var password:String="", 
    var name:String ="")

class FormHandlerSnippet {

  def render = {
     val responseForm = new MyFormResponse()
     "@email" #> SHtml.text("", (valueSupplied) => {
        responseForm.email = valueSupplied
     }) &
     "@pwd" #> SHtml.password("", (valueSupplied) => {
        responseForm.password = valueSupplied
     }) &
     "@name" #> SHtml.text("", (valueSupplied) => {
        responseForm.name = valueSupplied
     }) &
     "#submitbutton" #> SHtml.submit("Sign In", () => {
        S.redirectTo("/hello", () => ShowHelloSnippet.myVals(Full(responseForm)))
     })
  }
}

hello.html

<div data-lift="ShowHelloSnippet">
  <p>Hello <span name="paramname"></span></p>
</div>

Snippet

object ShowHelloSnippet {
  object myVals extends RequestVar[Box[MyFormResponse]](Empty)
}

class ShowHelloSnippet {
  def render = "*" #> {
    ShowHelloSnippet.myVals.get.map { r =>
      "@paramname" #> r.name
    }
  }
}

This will have the form set values on a object, then do a stateful redirect which sets the values in the ShowHelloSnippet for you to use after the page has been redirected. As an alternate to both, you could use Ajax to simply display the values on the same page.


Post a Comment for "How To Get Parameters In An HTML Page Passed From A Snippet With Lift Framework"