Different scripts and styles per page

13 views
Skip to first unread message

edgarchan

unread,
Nov 30, 2007, 3:59:39 PM11/30/07
to liftweb
Hi all.

Question about this:
Suppose i have many pages that requires different styles and scripts
but results too heavy to put them all on the decorator's head. How can
i load specific scripts on different templates?

Is there any tag that can be used to attach anything to the decorator
from the template?

something like <lift:bind name="content" /> or the opposite of
lift:snipped

After looking around lift examples and code i didn't find a hint. do i
missing something?

thanks.

David Pollak

unread,
Nov 30, 2007, 5:32:00 PM11/30/07
to lif...@googlegroups.com
Oddly enough, I was just dealing with a very similar issue.

The answer comes in two parts.

First, (after checking out head from SVN), you can do the following:
<lift:surround with="default" extra_script="foo"/>

The attributes passed are now available in S.attr("extra_script")

In your template, you can have a <lift:snippet type="Util:processExtraScript"/>

class Util {
  def processExtraScript = S.attr("extra_script").map(n => <script src={"/scripts/"+n+".js"}/>).openOr(Nil)
}

What this does is it allows you to define an extra script in the page that's surrounding itself.  In the surround template, you can process the attributes.

Sound reasonable?

Thanks,

David

http://liftweb.net
Collaborative Task Management http://much4.us

edgarchan

unread,
Nov 30, 2007, 10:30:55 PM11/30/07
to liftweb
Nice!! .. it works very well ... thank you for your fast and clear
advice. cheers!


On Nov 30, 4:32 pm, "David Pollak" <feeder.of.the.be...@gmail.com>
wrote:
> Oddly enough, I was just dealing with a very similar issue.
>
> The answer comes in two parts.
>
> First, (after checking out head from SVN), you can do the following:
> <lift:surround with="default" extra_script="foo"/>
>
> The attributes passed are now available in S.attr("extra_script")
>
> In your template, you can have a <lift:snippet
> type="Util:processExtraScript"/>
>
> class Util {
> def processExtraScript = S.attr("extra_script").map(n => <script
> src={"/scripts/"+n+".js"}/>).openOr(Nil)
>
> }
>
> What this does is it allows you to define an extra script in the page that's
> surrounding itself. In the surround template, you can process the
> attributes.
>
> Sound reasonable?
>
> Thanks,
>
> David
>
> On 11/30/07, edgarchan <edgarchancarri...@gmail.com> wrote:
>
>
>
>
>
> > Hi all.
>
> > Question about this:
> > Suppose i have many pages that requires different styles and scripts
> > but results too heavy to put them all on the decorator's head. How can
> > i load specific scripts on different templates?
>
> > Is there any tag that can be used to attach anything to the decorator
> > from the template?
>
> > something like <lift:bind name="content" /> or the opposite of
> > lift:snipped
>
> > After looking around lift examples and code i didn't find a hint. do i
> > missing something?
>
> > thanks.
>
> --
> lift, the secure, simple, powerful web frameworkhttp://liftweb.net
> Collaborative Task Managementhttp://much4.us

David Bernard

unread,
Jan 4, 2008, 3:59:13 AM1/4/08
to liftweb
With the version 0.4 (SNAPSHOT)you could declare <head>...</head>
inside you snippet (at any level) and the content will be moved to the
html/head section.

ex:
template :
<html>
<head>
<title>foo</title>
</head>
<body>
<lift:bind name="content" />
</body>
</html>

page :
<lift:surround with="default" at="content">
<head>
<script src="myscript.js"></script>
<style>
...
</style>
</head>
<h2>Welcome to the your project!</h2>
</lift:surround>

=>
<html>
<head>
<title>foo</title>
<script src="myscript.js"></script>
<style>
...
</style>
</head>
<body>
<h2>Welcome to the your project!</h2>
</body>
</html>

It's possible to have many head, and generate head from snippet.ù

/davidB

Viktor Klang

unread,
Jan 4, 2008, 5:21:51 AM1/4/08
to lif...@googlegroups.com
Hey DaveB!
 
I still think the <lift:inject>-suggestion looks way cleaner.
(though I agree that it takes more code and consideration to implement)
 
Cheers man!
 
-Viktor

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

Marius

unread,
Jan 4, 2008, 8:26:21 AM1/4/08
to liftweb
That's pretty cool !

Steve Jenson

unread,
Jan 5, 2008, 2:03:55 AM1/5/08
to lif...@googlegroups.com
I'd say!

On Jan 4, 2008 5:26 AM, Marius <marius...@gmail.com> wrote:
>
> That's pretty cool !
>
> >
>

Reply all
Reply to author
Forward
0 new messages