how to serve html fragments

56 views
Skip to first unread message

george

unread,
Apr 26, 2009, 8:02:25 AM4/26/09
to Lift
hello all,

hopefully someone can help me out here.

i am trying to port some simple ajax stuff over to lift from a rails
app. basically it just loads an html fragment and puts it into the dom
using prototype.

i have set up a template which contains the fragment at src/main/
webapp/fragment.html

<ul><li>item</li></ul>

then i made the page available using the SiteMap and all seems good,
but here comes the problem

the lift response adds the xml declaration and doctype, one of which
seems to cause prototype some problems

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ul><li>item</li></ul>

so the question is, how can i make lift send back the raw html without
meddling with it?

i have tried out using ResourceServer to serve it statically which
works, but this wouldn't allow me to generate the fragment
dynamically.

i would also prefer to have a separate html template file rather than
embedding the markup code in a snippet.

any thoughts gratefully received..

george

Timothy Perrett

unread,
Apr 26, 2009, 3:49:24 PM4/26/09
to Lift
George,

To tell lift what doctype you want to use see my blog post here:
http://is.gd/uJ4L

Also, you'll want to read another one of my posts in which I discuss
the bind(...) method and how you can stop putting markup into your
snippets: http://is.gd/sfyT

Cheers, Tim

fatu

unread,
May 20, 2009, 2:32:32 AM5/20/09
to Lift
Timothy,

thanks for the links, I found them useful and I find your blog in
general very interesting. Came across scala-blogs.org and it looks
quite promising as well.

I knew "bind" already from the "Exploring Lift" book which I pull from
git, build with Lyx and keep at hand regularly. In the doctype post,
though, I couldn't find a way to specify "no doctype" which I think
is necessary to serve a fragment; plus I couldn't find any other easy
"out-of-the-box" way to do it. Shouldn't this use case (serving
fragments), which I think is quite common, be better / more easily
supported by the framework? Can someone post an example of how to do
it with raw response handling in the meanwhile?

Thanks anybody.

Fabio

On 26 Apr, 21:49, Timothy Perrett <timo...@getintheloop.eu> wrote:
> George,
>
> To tell lift what doctype you want to use see my blog post here:http://is.gd/uJ4L
>
> Also, you'll want to read another one of my posts in which I discuss
> the bind(...) method and how you can stop putting markup into your
> snippets:http://is.gd/sfyT
>
> Cheers, Tim
>
> On Apr 26, 1:02 pm, george <geo...@mattandgeorge.com> wrote:
>
> > hello all,
>
> > hopefully someone can help me out here.
>
> > i am trying to port some simple ajax stuff over to lift from a rails
> > app. basically it just loads anhtmlfragmentand puts it into the dom
> > using prototype.
>
> > i have set up a template which contains thefragmentat src/main/
> > webapp/fragment.html
>
> > <ul><li>item</li></ul>
>
> > then i made the page available using the SiteMap and all seems good,
> > but here comes the problem
>
> > the lift response adds the xml declaration and doctype, one of which
> > seems to cause prototype some problems
>
> > <?xml version="1.0" encoding="UTF-8"?>
> > <!DOCTYPEhtmlPUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> > <ul><li>item</li></ul>
>
> > so the question is, how can i make lift send back the rawhtmlwithout
> > meddling with it?
>
> > i have tried out using ResourceServer to serve it statically which
> > works, but this wouldn't allow me to generate thefragment
> > dynamically.
>
> > i would also prefer to have a separatehtmltemplate file rather than

marius d.

unread,
May 20, 2009, 7:16:55 AM5/20/09
to Lift
As I understood you want to make an Ajax request and serve back a
Document Fragment. If so please also take a look at Jx stuff. We
discuss Jx classes in a fairly amount of details in the lift book.

Br's,
Marius

fatu

unread,
May 20, 2009, 8:13:26 AM5/20/09
to Lift
Hi Marius,

as I understand them, Jx classes help in generating JS that can itself
generate DOM at the client. Am I wrong? I'd really like to serve back
an HTML fragment built by using the normal Lift template pipeline (so
including surround & bind). Is that possible?

Thanks

David Pollak

unread,
May 20, 2009, 10:05:07 AM5/20/09
to lif...@googlegroups.com
I'm about to commit S.skipDocType = true | false.  If you set it to true, the <!DOCTYPE .../> will be omitted from the response page.  This will allow your AJAX fragements to pull parts of pages from the server.  Note that someplace in your snippets, you'll have to set S.skipDocType = true.

Does this address your issue?
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

Timothy Perrett

unread,
May 20, 2009, 11:08:33 AM5/20/09
to lif...@googlegroups.com

Neat! Not sure how it is that we have no come across this need before! Lol.

Cheers, Tim


On 20/05/2009 15:05, "David Pollak" <feeder.of...@gmail.com> wrote:

I'm about to commit S.skipDocType = true | false.  If you set it to true, the <!DOCTYPE .../> will be omitted from the response page.  This will allow your AJAX fragements to pull parts of pages from the server.  Note that someplace in your snippets, you'll have to set S.skipDocType = true.

Does this address your issue?

On Wed, May 20, 2009 at 5:13 AM, fatu <fab...@gmail.com> wrote:

Hi Marius,

as I understand them, Jx classes help in generating JS that can itself
generate DOM at the client.  Am I wrong? I'd really like to serve back
an HTML fragment built by using the normal Lift template pipeline (so
including surround & bind). Is that possible?

Thanks

On 20 Mag, 13:16, "marius d." <marius.dan...@gmail.com> wrote:
> As I understood you want to make an Ajax request and serve back a
> Document Fragment. If so please also take a look at Jx stuff. We
> discuss Jx classes in a fairly amount of details in the lift book.
>
> Br's,
> Marius
>
> On May 20, 9:32 am, fatu <fab...@gmail.com> wrote:
>
> > Timothy,
>
> > thanks for the links, I found them useful and I find your blog in
> > general very interesting. Came across scala-blogs.org <http://scala-blogs.org>  and it looks

fatu

unread,
May 20, 2009, 11:38:28 PM5/20/09
to Lift
Hi David,

yes it will do, thank you so much for such a lightning-fast
development in reply!

I'm only thinking about a case that is probably much more of an
exception than a rule, i.e. when the snippet dynamically decides if it
needs to generate a full page or a fragment (which is not a very good
design anyway IMHO), for example based on some info passed. But even
in that case the snippet will know that and will be able to set
S.skipDocType = true accordingly, so it should be perfectly fine. Plus
it won't be difficult at all to make the processing diverge in the 2
cases by using URL rewrites, redirecting it to a different template /
snippet depending on parameters or URL structure, which is probably a
better design too.

Again many thanks everyone and David especially!

On May 20, 4:05 pm, David Pollak <feeder.of.the.be...@gmail.com>
wrote:
> Beginning Scalahttp://www.apress.com/book/view/1430219890

David Pollak

unread,
May 21, 2009, 12:42:21 AM5/21/09
to lif...@googlegroups.com
On Wed, May 20, 2009 at 8:38 PM, fatu <fab...@gmail.com> wrote:

Hi David,

yes it will do, thank you so much for such a lightning-fast
development in reply!

I'm only thinking about a case that is probably much more of an
exception than a rule, i.e. when the snippet dynamically decides if it
needs to generate a full page or a fragment (which is not a very good
design anyway IMHO), for example based on some info passed. But even
in that case the snippet will know that and will be able to set
S.skipDocType = true accordingly, so it should be perfectly fine. Plus
it won't be difficult at all to make the processing diverge in the 2
cases by using URL rewrites, redirecting it to a different template /
snippet depending on parameters or URL structure, which is probably a
better design too.

You could also pass a header flag from your AJAX call.  If the header's set, you don't do the boilerplate surround that'll include all the <html>, <head> and <body> tags.

Anyway, enjoy and thanks for the use case.

Party on.



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890

fatu

unread,
May 21, 2009, 3:27:33 AM5/21/09
to Lift
Right, didn't think about that alternative.

Thanks and best compliments to you and all the active Lift community
for this superb Scala framework! I really think (and hope) it's going
to have a big impact and a tremendous popularity rise in the next
months, especially with more forthcoming books and articles spreading
the word.

george

unread,
May 21, 2009, 5:15:23 AM5/21/09
to Lift
Thanks guys for following up on this.

It's a great help here too.

George

On May 21, 5:42 am, David Pollak <feeder.of.the.be...@gmail.com>
wrote:

David Pollak

unread,
May 21, 2009, 9:38:08 AM5/21/09
to lif...@googlegroups.com
On Thu, May 21, 2009 at 12:27 AM, fatu <fab...@gmail.com> wrote:

Right, didn't think about that alternative.

Thanks and best compliments to you and all the active Lift community
for this superb Scala framework! I really think (and hope) it's going
to have a big impact and a tremendous popularity rise in the next
months, especially with more forthcoming books and articles spreading
the word.

We ask two things: build cool apps in Lift and be part of this community, asking and answering questions.

Thanks!



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890

fatu

unread,
Jun 3, 2009, 10:40:45 AM6/3/09
to Lift
...Another question for now :-)

Does it work for views as well?

Many thanks.

Fabio

David Pollak

unread,
Jun 3, 2009, 11:57:13 AM6/3/09
to lif...@googlegroups.com
On Wed, Jun 3, 2009 at 7:40 AM, fatu <fab...@gmail.com> wrote:

...Another question for now :-)

Does it work for views as well?

It should.  Views are treated just like static XHTML files... a source of NodeSeq
 



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890

fatu

unread,
Jun 4, 2009, 3:10:23 AM6/4/09
to Lift
Yes it does indeed. Sorry, I was getting confused by the fact that
Firefox still doesn't like the fragment but it happens both when using
a view and when using a template + snippet. The DOCTYPE is indeed
missing in both cases when "S.skipDocType" is on, I guess what's still
causing trouble is the fact that the XML declaration is still there.

I think I can remember seeing somewhere (group? blogs?) another flag
that, when turned on, would skip the XML declaration but I can't find
it right now. Is there really such a thing or did I only dream about
it?

Thanks.

David Pollak

unread,
Jun 4, 2009, 9:57:40 AM6/4/09
to lif...@googlegroups.com
On Thu, Jun 4, 2009 at 12:10 AM, fatu <fab...@gmail.com> wrote:

Yes it does indeed. Sorry, I was getting confused by the fact that
Firefox still doesn't like the fragment but it happens both when using
a view and when using a template + snippet. The DOCTYPE is indeed
missing in both cases when "S.skipDocType" is on, I guess what's still
causing trouble is the fact that the XML declaration is still there.

I think I can remember seeing somewhere (group? blogs?) another flag
that, when turned on, would skip the XML declaration but I can't find
it right now. Is there really such a thing or did I only dream about
it?

Lemme see what I can do.



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890

David Pollak

unread,
Jun 4, 2009, 12:33:24 PM6/4/09
to lif...@googlegroups.com
Okay... checked in a change that removes the <?...?> XML header as well

Timothy Perrett

unread,
Jun 4, 2009, 1:04:45 PM6/4/09
to lif...@googlegroups.com

Have you pushed this change? Its not showing on github?

Cheers, Tim


On 04/06/2009 17:33, "David Pollak" <feeder.of...@gmail.com> wrote:

David Pollak

unread,
Jun 4, 2009, 1:06:57 PM6/4/09
to lif...@googlegroups.com
On Thu, Jun 4, 2009 at 10:04 AM, Timothy Perrett <tim...@getintheloop.eu> wrote:

Have you pushed this change? Its not showing on github?

I pushed, but forgot to commit... :-)  That's me, I just can't commit.

All committed and pushed.
 


Cheers, Tim


On 04/06/2009 17:33, "David Pollak" <feeder.of...@gmail.com> wrote:

Okay... checked in a change that removes the <?...?> XML header as well


fatu

unread,
Jun 5, 2009, 4:58:06 AM6/5/09
to Lift
I've had a look at your commit and skipDocType should now remove the
decl. as well. I'll try it out as soon as I can get a snapshot build
of it.

Many many thanks so far.

Fabio

On 4 Giu, 19:06, David Pollak <feeder.of.the.be...@gmail.com> wrote:
> On Thu, Jun 4, 2009 at 10:04 AM, Timothy Perrett <timo...@getintheloop.eu>wrote:
>
>
>
> > Have you pushed this change? Its not showing on github?
>
> I pushed, but forgot to commit... :-)  That's me, I just can't commit.
>
> All committed and pushed.
>
>
>
> > Cheers, Tim
>
> > On 04/06/2009 17:33, "David Pollak" <feeder.of.the.be...@gmail.com> wrote:
>
> > Okay... checked in a change that removes the <?...?> XML header as well
>
> --
> Lift, the simply functional web frameworkhttp://liftweb.net
> Beginning Scalahttp://www.apress.com/book/view/1430219890

fatu

unread,
Jun 5, 2009, 10:37:45 AM6/5/09
to Lift
Fantastic, it works great!

I'm getting an unrelated exception, perhaps that's something in flush
(looks a bit like it from the trace) and you already know but just in
case:

java.lang.NoSuchFieldException: refSet
at java.lang.Class.getDeclaredField(Class.java:1882)
at net.liftweb.http.PointlessActorToWorkAroundBug$$anonfun$act$1$
$anonfun$apply$1.apply(LiftServlet.scala:715)
at net.liftweb.http.PointlessActorToWorkAroundBug$$anonfun$act$1$
$anonfun$apply$1.apply(LiftServlet.scala:707)
at scala.actors.Reaction.run(Reaction.scala:78)
at net.liftweb.http.ActorSchedulerFixer$$anon$1$$anonfun$execute
$1.apply(LiftServlet.scala:668)
at net.liftweb.http.ActorSchedulerFixer$$anon$1$$anonfun$execute
$1.apply(LiftServlet.scala:668)
at net.liftweb.http.ActorSchedulerFixer$$anonfun$20$$anon$2.run
(LiftServlet.scala:626)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask
(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run
(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)

Thanks again.

Fabio

David Pollak

unread,
Jun 5, 2009, 12:10:25 PM6/5/09
to lif...@googlegroups.com
Are you using Scala 2.7.4 or 2.7.5?
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890

fatu

unread,
Jun 6, 2009, 9:40:54 AM6/6/09
to Lift
2.7.5 right now (and a fresh maven repo too) but I think (not sure
though) I had it with 2.7.4 as well. Seems to expect a refSet field in
ActorGC which i couldn't find in 2.7.5 sources.

Fabio

David Pollak

unread,
Jun 6, 2009, 9:58:48 AM6/6/09
to lif...@googlegroups.com

Lift is 2.7.4.  We have not upgraded to 2.7.5 as the delta is actor fixes in response to lift-related tickets that we solved with our own actor impl.  I'm talking to Philipp Haller today at the Lift Off about how to proceed.

On Jun 6, 2009 6:49 AM, "fatu" <fab...@gmail.com> wrote:


2.7.5 right now (and a fresh maven repo too) but I think (not sure
though) I had it with 2.7.4 as well. Seems to expect a refSet field in
ActorGC which i couldn't find in 2.7.5 sources.

Fabio

On 5 Giu, 18:10, David Pollak <feeder.of.the.be...@gmail.com> wrote: > Are you using Scala 2.7.4 or...

> On Fri, Jun 5, 2009 at 7:37 AM, fatu <fab...@gmail.com> wrote: > > > Fantastic, it works great! > ...

fatu

unread,
Jun 6, 2009, 11:13:19 AM6/6/09
to Lift
Thanks a lot for the info David, then I'll consider reverting to 2.7.4
until Lift integrates the latest Scala Actors lib (and fixes).

Fabio

David Persons

unread,
Jun 28, 2009, 8:39:12 PM6/28/09
to Lift
I spend a lot of time today on (close to) the same problem.. IE 6 was
not showing my site in strict mode.. with the strict doctype set
correctly I thought.. :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

The problem was the order of the two lines.. Seems that to have IE
pick up the correct doctype, the html HAS TO START with the doctype.
Changing the order makes IE show my website perfectly:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?xml version="1.0" encoding="UTF-8"?>

How to change the order in my code so that Lift spits out my pages
correctly?

regards,
David Persons

David Persons

unread,
Jun 29, 2009, 3:20:28 AM6/29/09
to Lift
The problem seems to be a known stupid bug in IE6.. see:
http://www.webdeveloper.com/forum/showthread.php?t=1658

David Pollak

unread,
Jun 29, 2009, 1:47:45 PM6/29/09
to lif...@googlegroups.com
I just pushed a patch that will flip the DocType and <?xml version="1.0" encoding="UTF-8"?> if the browser is IE6
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Reply all
Reply to author
Forward
0 new messages