Lift "View"

7 views
Skip to first unread message

Marius

unread,
Jan 25, 2008, 8:57:55 AM1/25/08
to liftweb
Hi,

Forgive me for asking but what exactly is the role of the view folder
when generating a lift project? The lift example application contains
a class XMLFun that defines a render function. Is this invoked by lift
automatically? Can you please elaborate?


Br's,
Marius

David Pollak

unread,
Jan 25, 2008, 9:53:42 AM1/25/08
to lif...@googlegroups.com
Marius,

You can have static templates that live on disk.  Useful if you've got designers in your process.

You can also have Scala code that renders your templates.  Useful for ERB/JSP style coding.

Put those templates in your "view" package.  Calls to the templates will be made based on convention:

/foo/bar

Will looks for the view.Foo class and dispatch to bar.

If your view extends InsecureLiftView, lift will look for the bar method and invoke it.  This is part of the dangers of taking stuff off the wire... you're exposing all the methods in your class to invocation.

If your view extends LiftView, you have to implement the dispatch_& method (or value) which returns a partial function that pattern matches on a string and returns the function that will return your rendered page.

Does that help?

Thanks,

David
--
lift, the secure, simple, powerful web framework http://liftweb.net
Collaborative Task Management http://much4.us

Marius

unread,
Jan 25, 2008, 10:19:45 AM1/25/08
to liftweb
As always ... thank you very much !

Perhaps it would worth to have this kind of info on liftweb site.
Maybe it exists but somehow I didn't find it and I had to ask ..

Br's,
Marius

On Jan 25, 4:53 pm, "David Pollak" <feeder.of.the.be...@gmail.com>
wrote:
> Marius,
>
> You can have static templates that live on disk. Useful if you've got
> designers in your process.
>
> You can also have Scala code that renders your templates. Useful for
> ERB/JSP style coding.
>
> Put those templates in your "view" package. Calls to the templates will be
> made based on convention:
>
> /foo/bar
>
> Will looks for the view.Foo class and dispatch to bar.
>
> If your view extends InsecureLiftView, lift will look for the bar method and
> invoke it. This is part of the dangers of taking stuff off the wire...
> you're exposing all the methods in your class to invocation.
>
> If your view extends LiftView, you have to implement the dispatch_& method
> (or value) which returns a partial function that pattern matches on a string
> and returns the function that will return your rendered page.
>
> Does that help?
>
> Thanks,
>
> David

David Pollak

unread,
Jan 25, 2008, 10:28:01 AM1/25/08
to lif...@googlegroups.com
On 1/25/08, Marius <marius...@gmail.com> wrote:

As always ... thank you very much !

Perhaps it would worth to have this kind of info on liftweb site.
Maybe it exists but somehow I didn't find it and I had to ask ..

Yeah.  There are a bunch of issues with me putting the stuff up:
  • MediaWiki and I don't get along.  ctrl-e (end of line for my emacs fingers) throws away posts and that messes me up.
  • I generally do stream of consciousness when I write except when I'm answering specific questions and the streams are long, twisted, and often not helpful
  • My ability to categorize and organize on the wiki is limited (because everything becomes a stream of consciousness)
DavidB was doing a great job of organizing the wiki and transcribing my rabblings, but he's got other constraints on his time.

Not the best answer. Sigh.


Marius

unread,
Jan 25, 2008, 10:52:31 AM1/25/08
to liftweb
I see ...

Anyways I just tried to do a dummy view like:

package com.pkg.view;
import net.liftweb.http._


class TestView extends InsecureLiftView{

def render = {
<hello></hello>
}
}

and tried to invoke it like: http://localhost:8080/lift/TestView/render
but lift automatically renders the home page

I also built the example application and when clicking the XMLFun link
I'm getting

<html>
<body>
The Requested URL /xml_fun was not found on this server
</body>
</html>


although the XML Fun link works on the online liftweb demo
application.

I'm using 0.5 snapshot so are there some things broken here or am I
doing something wrong?

Br's,
Marius

Marius

unread,
Jan 25, 2008, 11:14:31 AM1/25/08
to liftweb
I also tried http://localhost:8080/lift/test_view/render but same
thing home page was rendered ...

TylerWeir

unread,
Jan 25, 2008, 11:18:53 AM1/25/08
to liftweb
> Perhaps it would worth to have this kind of info on liftweb site.

http://liftweb.net/index.php/FAQ#What.27s_the_.22View.22_folder_for.3F

Done and done.

Viktor Klang

unread,
Jan 25, 2008, 11:19:22 AM1/25/08
to lif...@googlegroups.com
IMHO if an URL does not exist it should return a 404
 
-V

 
On 1/25/08, Marius <marius...@gmail.com> wrote:

I also tried http://localhost:8080/lift/test_view/render but same
thing home page was rendered ...

/                                                                 \
        /lift/ committer (www.liftweb.net)
      SGS member (Scala Group Sweden)
  SEJUG member (Swedish Java User Group)
\_____________________________________/

Marius

unread,
Jan 25, 2008, 11:28:25 AM1/25/08
to liftweb

Thank you very much guys ... but still why TestView#render function is
not invoked and I'm getting the home page? I agree with Viktor that if
a link does not work 404 should be returned. But even so maybe I'm
doing something wrong although using views is pretty straight forward
but apparently it is not working for me.

Br's,
Marrius

David Pollak

unread,
Jan 25, 2008, 11:42:12 AM1/25/08
to lif...@googlegroups.com
It's a bug.

I introduced it when I moved to the InsecureLiftView code.

I'll fix it today.

Marius

unread,
Jan 25, 2008, 11:58:00 AM1/25/08
to liftweb
Wonderful !!! ... I'll checkout tomorrow ...

Personally the value that I see in view is that it's pretty easy to
build REST API's (... also possible with dispatch pattial functions)
but this is a bit simpler. Different security flavors can still be
used even with InsecureLiftView through ...

Br's,
Marius

David Pollak

unread,
Jan 25, 2008, 12:20:12 PM1/25/08
to lif...@googlegroups.com
Rock and Roll!

Thanks!

David Pollak

unread,
Jan 25, 2008, 12:25:08 PM1/25/08
to lif...@googlegroups.com

Yeah... with the recent S.setHeader(name, value), you can alter the
response headers based on whatever to give you a lot of dynamic
rendering capabilities.

The fix is checked in.
> Br's,
> Marius
> >
>

Marius

unread,
Jan 26, 2008, 4:44:22 AM1/26/08
to liftweb
hmm .. something is odd here ...


I just checked out the latest sources and did a lift build. It still
doesn't work for for me. I'm still redirected to home page and
error_report gives me URL not found.

I also tried with:

package com.home.view;

import scala.xml._
import net.liftweb.http._
import net.liftweb.util._

class TestView extends LiftView {

def dispatch_& = {
case "index" => render _
}

def render = Full(<hi>Something</hi>)
}

XMLFun from the example app works fine but still what am I doing
wrong?

I tried urls:

http://localhost:808/lifttest/TestView/index
http://localhost:808/lifttest/TestView
http://localhost:808/lifttest/test_view
http://localhost:808/lifttest/test_view/index

etc.


no luck....


Any idea ?

Br's,
Marius

Marius

unread,
Jan 26, 2008, 4:53:19 AM1/26/08
to liftweb
my lift dummy app is not the root web app ... it's in lifttest
context. That shouldn't matter anyways ... but maybe when lift parses
the URL ? ... that's the only difference that I see between my example
and the lift example app.

Marius

unread,
Jan 26, 2008, 5:23:16 AM1/26/08
to liftweb
Ok .. I believe I found why it's not working. I was defining a sitemap
in Boot and it seems that sitemap navigation rules somehow shadows the
views and the views are not executed. The lilft example app never
defines a sitemap. Once I removed the sitemap from my app the views
are working with no problem.

I think that views should work even if sitemaps are defines and to my
mind this looks like a bug. Or is this behavior on purpose?


Br's,
Marius

David Bernard

unread,
Jan 26, 2008, 6:00:13 AM1/26/08
to lif...@googlegroups.com
Did you define the url of the view in the Sitemap ?

/davidB

David Pollak

unread,
Jan 26, 2008, 8:31:37 AM1/26/08
to lif...@googlegroups.com
Marius,

SiteMap rules are applied before the view is retrieved, thus unless you
define the URL of the view in your sitemap, the view will not be displayed.

SiteMap is intended to be access control for all the pages that
"normally rendered" (gotta find a better name... basically the pages
that are rendered by the normal flow of page rendering rather than by a
pattern match in the dispatch table) must match the rules defined in
SiteMap.

Thanks,

David

Marius

unread,
Jan 26, 2008, 8:57:20 AM1/26/08
to liftweb
Ok ... got it. Somehow I though that view are kind of decoupled by the
SiteMap rules.


Another question. Suppose I'm using a view to return plain XML not
html or xhtml. Is there a way to specify XMLResponse instead on
XhtmlResponse ? I wouldn't like the returned XML from my view to NOT
contain

<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">

(I know hat in this case should probably use a dispatch but I'm just
curious)

Br's,
Marius

David Pollak

unread,
Jan 26, 2008, 9:32:04 AM1/26/08
to lif...@googlegroups.com
On 1/26/08, Marius <marius...@gmail.com> wrote:

Ok ... got it. Somehow I though that view are kind of decoupled by the
SiteMap rules.


Another question. Suppose I'm using a view to return plain XML not
html or xhtml. Is there a way to specify XMLResponse instead on
XhtmlResponse ? I wouldn't like the returned XML from my view to NOT
contain

<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">

As of my latest checkin:

S.setDocType(Empty) // no doctype header
 

(I know hat in this case  should probably use a dispatch but I'm just
curious)

Br's,
Marius



--
lift, the secure, simple, powerful web framework http://liftweb.net

Marius

unread,
Jan 26, 2008, 10:47:23 AM1/26/08
to liftweb
Yup.. I just checked out your updates and everything goes smooth now.


Thanks again...


Br's,
Marius
Reply all
Reply to author
Forward
0 new messages