Lift dynamic node attributes

40 views
Skip to first unread message

Marius

unread,
Feb 2, 2008, 5:00:30 AM2/2/08
to liftweb
Hi,


Here is a situation. I have a html page with bunch of tags in it. But
I'd like to set some dynamic values for tags attributes. For instance:

<div dir="<dynamic value here>">

</div>
(we can extrapolate this to setting dynamically the dir attribute for
html tag)

I don't think I can do this currently unless:

1. I'm wrapping my div by a snippet declaration and within the snipped
I can do whatever I want with the div node including setting dir
attribute based on whatever

2. Maybe there are some hooks over there that allows me to "fix" the
markup right before sending it to client.

I'm NOT proposing a JSTL/JSF like approach like:

<div dir="${Class.method}">

</div>

(I would very much like to avoid such constructs as they are pretty
limited)

Any thoughts?

Br's,
Marius

David Bernard

unread,
Feb 2, 2008, 5:55:53 AM2/2/08
to lif...@googlegroups.com
I like the snippet approach.


But you have 2 others solutions (not tested):
* using javascript to set the dir of the div (the selection of the div could be done by a static class or an id)
* using css to set the dir (ex: { direction: ltr;})
then you could create a snippet (call from you template or only the page) a snippet that only generate
<head> ... </head> NodeSeq (with css or js code). the <head> NodeSeq will be moved/injected into the html/head of the final page.

/davidB

Marius

unread,
Feb 2, 2008, 6:07:19 AM2/2/08
to liftweb
Thanks David,

The Javascript solution may not be very handy as the state is kept on
server side and this may lead to more code ... The css approach is not
very reliable. From my experience with various browsers css direction
and dir attribute behaves differently and dir attribute seems more
widely adopted.

As I see it with JS:
1. A snipped would dynamically add a javascript reference to header
and as you said will be merged with the template head
2. Perhaps define an onready function that would change the dir
attribute say of the html tag ... but wouldn't this lead to un
undesired visual effects? I mean the page is loaded progressively and
on ready is called when the DOM tree is complete. Seems to me that
there is a catch here no matter what.

So being "lazy" that's probably too much work jut for achieving
"dynamic attributes". :)

Br's,
Marius

On Feb 2, 12:55 pm, "David Bernard" <david.bernard...@gmail.com>
wrote:
> I like the snippet approach.
>
> But you have 2 others solutions (not tested):
> * using javascript to set the dir of the div (the selection of the div could
> be done by a static class or an id)
> * using css to set the dir (ex: { direction: ltr;})
> then you could create a snippet (call from you template or only the page) a
> snippet that only generate
> <head> ... </head> NodeSeq (with css or js code). the <head> NodeSeq will be
> moved/injected into the html/head of the final page.
>
> /davidB
>

David Bernard

unread,
Feb 2, 2008, 6:55:03 AM2/2/08
to lif...@googlegroups.com
OK
The js code isn't lot of code : something like :
$(function() {
// code to execute when the DOM is ready
$(".dirdiv").attr("dir", "ltr")
});
 and every node with class "dirdiv" have attribute "dir" set to "ltr"

other solution : transform (on serverside) the response by adding the dir attribute. The transformation could be done by :
* a servlet filter
* a surround snippet (transform its incoming parameter) (not tested)
* a not available lift feature : PageTransformer, but you could request it for futur ;)

/davidB

Marius

unread,
Feb 2, 2008, 7:06:55 AM2/2/08
to liftweb
I like your answer David :)

But in your Js example assume that by default the pages is has
dir=ltr and then in onready function execute your code. But by the
time the function is executed we can see part of the page in the
browser and cold see the page rendered ltr become right to left. And
this transformation from ltr to rtl maybe visible to the user and I
don't think this is a desired behavior. Please correct me if I'm
wrong.

I'm very interested what David P. feels about your proposal regarding
PageTransformer. I kind of like it ;)

Br's,
Marius

On Feb 2, 1:55 pm, "David Bernard" <david.bernard...@gmail.com> wrote:
> OK
> The js code isn't lot of code : something like :
>
> $(function() {
> // code to execute when the DOM is ready
> $(".dirdiv").attr("dir", "ltr")
> });
>
> and every node with class "dirdiv" have attribute "dir" set to "ltr"
>
> other solution : transform (on serverside) the response by adding the dir
> attribute. The transformation could be done by :
> * a servlet filter
> * a surround snippet (transform its incoming parameter) (not tested)
> * a not available lift feature : PageTransformer, but you could request it
> for futur ;)
>
> /davidB
>

Marius

unread,
Feb 2, 2008, 7:47:07 AM2/2/08
to liftweb
I just tried the wrapper snippet approach. I put my template within a
wrap snippet and the snippet looks like:

def wrap(xhtml: Group): NodeSeq = {

xhtml map (v => v match {
case (<html>{_*}</html>) => Elem(v.prefix, v.label, ("dir" ->
"rtl"), v.scope, v.child:_*)
case _ => v
})
}

it works pretty well. Although I'm very interested to know your
thoughts regarding this or if there are better/simpler ways of
achieving this. Appreciate it !


Br's,
Marius

David Bernard

unread,
Feb 2, 2008, 8:02:00 AM2/2/08
to lif...@googlegroups.com
Could you add a page on the wiki (tips section) ?
A more "general" solution should take care of existing attribute on <html>, isn't it ?

/davidB

Marius

unread,
Feb 2, 2008, 9:07:42 AM2/2/08
to liftweb
Yes David you are quite right. I've added
http://liftweb.net/index.php/HowTo_decorate_your_markup_with_dynamic_attributes.

Anyone please feel free to change it if there are better ways.

Br's,
Marius

David Bernard

unread,
Feb 2, 2008, 9:17:35 AM2/2/08
to lif...@googlegroups.com
Thx, nice tips

Note: You mix feature 0.4 (jquery-1.2.1) and 0.5-SNAPSHOT (Lift:Hello.wrap) :)

/davidB

Marius

unread,
Feb 2, 2008, 9:22:08 AM2/2/08
to liftweb
Good catch :) .. I replaced jquery-1.2.1 to jquery ...not that this
matters too much in this example

Br's,
Marius

David Bernard

unread,
Feb 2, 2008, 9:33:02 AM2/2/08
to lif...@googlegroups.com
I do a little changes:
* add [[Category:Lift-0.5]]
* change the path to jquery and add id attribute (in 0.5 jquery is in the jar, script directory could be used to override)

/davidB

Marius

unread,
Feb 2, 2008, 9:56:43 AM2/2/08
to liftweb
thanks a bunch !

! Marius

David Pollak

unread,
Feb 2, 2008, 10:40:56 AM2/2/08
to lif...@googlegroups.com
<div lift:snippet="MyDivThing:calcDir"> ... </div>

class MyDivThing {
  def hereDiv = new UnprefixedAttribute("dir", "fruitbat", Null)

Marius

unread,
Feb 2, 2008, 11:22:30 AM2/2/08
to liftweb
Coooooooooooooooooooooooooool ! ... I added this on the wiki. I hope
you don't mind.

THANK YOU !... as I suspected you always have something simpler :)

Br's,
Marius

On Feb 2, 5:40 pm, "David Pollak" <feeder.of.the.be...@gmail.com>
wrote:
> --
> lift, the secure, simple, powerful web frameworkhttp://liftweb.net
> Collaborative Task Managementhttp://much4.us

Viktor Klang

unread,
Feb 2, 2008, 6:21:05 PM2/2/08
to lif...@googlegroups.com
Great feature!

I propose to add an alias to call it "lift:attr" because that's more intuitive than lift:snippet.

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

David Pollak

unread,
Feb 2, 2008, 6:51:16 PM2/2/08
to lif...@googlegroups.com


Viktor Klang wrote:
Great feature!

I propose to add an alias to call it "lift:attr" because that's more intuitive than lift:snippet.
I'm going to bring it into line with the other lift snippet stuff so you'll be able to say:

<div lift:foo.bar="baz">

the bar method on Foo will be called with the Seq[Node] = Text("baz")

That won't happen before 0.5 is frozen, but will happen soon after.

Any use of lift:snippet="...." will work as advertised and lift:when is a special case that will not be changed.

Thanks,

David

Viktor Klang

unread,
Feb 2, 2008, 7:39:37 PM2/2/08
to lif...@googlegroups.com
On Feb 3, 2008 12:51 AM, David Pollak <d...@athena.com> wrote:


Viktor Klang wrote:
Great feature!

I propose to add an alias to call it "lift:attr" because that's more intuitive than lift:snippet.
I'm going to bring it into line with the other lift snippet stuff so you'll be able to say:

<div lift:foo.bar="baz">

the bar method on Foo will be called with the Seq[Node] = Text("baz")

That won't happen before 0.5 is frozen, but will happen soon after.

Cool beans :)

-V
 

Marius

unread,
Feb 3, 2008, 4:06:44 AM2/3/08
to liftweb
Hi,

Sorry for asking but what exactly lift:when does? I could not find it
on lift wiki. Looking into lift source idAndWhen is used for matching
commet tags?

Br's,
Marius

On Feb 3, 1:51 am, David Pollak <d...@athena.com> wrote:
> Viktor Klang wrote:
> > Great feature!
>
> > I propose to add an alias to call it "lift:attr" because that's more
> > intuitive than lift:snippet.
>
> I'm going to bring it into line with the other lift snippet stuff so
> you'll be able to say:
>
> <div lift:foo.bar="baz">
>
> the bar method on Foo will be called with the Seq[Node] = Text("baz")
>
> That won't happen before 0.5 is frozen, but will happen soon after.
>
> Any use of lift:snippet="...." will work as advertised and lift:when is
> a special case that will not be changed.
>
> Thanks,
>
> David
>
>
>
> > Cheers,
> > -V
>
> > On Feb 2, 2008 5:22 PM, Marius <marius.dan...@gmail.com
> > <mailto:marius.dan...@gmail.com>> wrote:
>
> > Coooooooooooooooooooooooooool ! ... I added this on the wiki. I hope
> > you don't mind.
>
> > THANK YOU !... as I suspected you always have something simpler :)
>
> > Br's,
> > Marius
>
> > On Feb 2, 5:40 pm, "David Pollak" <feeder.of.the.be...@gmail.com
> > <mailto:feeder.of.the.be...@gmail.com>>
> > wrote:
> > > <div lift:snippet="MyDivThing:calcDir"> ... </div>
>
> > > class MyDivThing {
> > > def hereDiv = new UnprefixedAttribute("dir", "fruitbat", Null)
>
> > > }
>
> > > On 2/2/08, Marius <marius.dan...@gmail.com
> > <mailto:marius.dan...@gmail.com>> wrote:
>
> > > > thanks a bunch !
>
> > > > ! Marius
>
> > > --
> > > lift, the secure, simple, powerful web frameworkhttp://liftweb.net
> > > Collaborative Task Managementhttp://much4.us
>
> > --
> > _____________________________________
> > / \
> > /lift/ committer (www.liftweb.net<http://www.liftweb.net>)

Marius

unread,
Feb 3, 2008, 5:57:03 AM2/3/08
to liftweb
Oh as I can see it is about setting the polling interval for Comet.
Correct?

David Pollak

unread,
Feb 3, 2008, 7:47:25 AM2/3/08
to lif...@googlegroups.com
On Feb 3, 2008 2:57 AM, Marius <marius...@gmail.com> wrote:

Oh as I can see it is about setting the polling interval for Comet.
Correct?

yes,

Consider it a reserved word.

Collaborative Task Management http://much4.us
Reply all
Reply to author
Forward
0 new messages