Layouts and JavaScript

23 views
Skip to first unread message

Scott LaBounty

unread,
Jun 5, 2009, 8:32:20 AM6/5/09
to ram...@googlegroups.com
What's the best way to include page specific JavaScript in a layout. My thought was in the controller do something along the lines of:

@page_javascript = "page1.js"

and then in the layout do something like:

<script src="#@page_javascript" type="text/javascript"></script>

Would this be the way to do something like this? I realized in my last post that I had included some javascript for a single page in the layout and that probably wasn't the right thing to do.

--
Scott
http://steamcode.blogspot.com/

james

unread,
Jun 5, 2009, 10:59:35 AM6/5/09
to Ramaze
On Jun 5, 6:32 am, Scott LaBounty <slabou...@gmail.com> wrote:
> What's the best way to include page specific JavaScript in a layout. My
> thought was in the controller do something along the lines of:
>
> @page_javascript = "page1.js"
>
> and then in the layout do something like:
>
> <script src="#@page_javascript" type="text/javascript"></script>

Can't <script></script> tags be added anywhere? They shouldn't have
to be at the top of your layout. You could place page specific
javascript between <script></script> tags at the bottom of the view
perhaps.

If you have page specific javascript, and it isn't a boat-load of js,
then perhaps just include it on all pages. It'll get cached anyway.
Look at how large jquery is, and people include that site-wide!

Sorry if I'm mis-understanding what you're trying to do. Hope this
helps.

James

James

Phrogz

unread,
Jun 5, 2009, 11:37:11 AM6/5/09
to Ramaze
On Jun 5, 6:32 am, Scott LaBounty <slabou...@gmail.com> wrote:
> What's the best way to include page specific JavaScript in a layout. My
> thought was in the controller do something along the lines of:
>
> @page_javascript = "page1.js"
>
> and then in the layout do something like:
>
> <script src="#@page_javascript" type="text/javascript"></script>

I've done this myself in the past. I've also done the following:
For page-specific CSS, instead of separate CSS sheets, I have this in
my layout:
<body id="<%=@page_id%>" class="<%=page_class%>">
where the id is usually the action and the class I set commonly
amongst similar pages. Then I have a single CSS file that's a little
bigger, but cached, with content like:
/* common css */
...

/* category-specific */
body.admin ...
body.admin ...

/* page-specific */
body#login ...
body#login ...

With jQuery, you can do the same thing by prefixing your selectors
that latch to objects the same say:

$(function(){
$('body#login #login_form').submit(function(){
// ...
});
})

Scott LaBounty

unread,
Jun 5, 2009, 6:40:05 PM6/5/09
to ram...@googlegroups.com
Phrogz,

OK, that's pretty cool. Thanks for the tip.

Scott
--
Scott
http://steamcode.blogspot.com/

jesusisramaz...@geoshell.com

unread,
Jun 5, 2009, 10:56:01 AM6/5/09
to ram...@googlegroups.com
2009/6/5 Scott LaBounty - slab...@gmail.com:

> @page_javascript = "page1.js"
>
> and then in the layout do something like:
>
> <script src="#@page_javascript" type="text/javascript"></script>

I haven't tried it, but that looks like it should work. But see also
the "js" method, as exemplified in the prototype given with "ramaze
create". It's a shortcut to make "<script src....>".

--
Pistos
http://blog.purepistos.net

Scott LaBounty

unread,
Jun 5, 2009, 8:46:33 PM6/5/09
to ram...@googlegroups.com
Pistos,

Thanks, but I'm not exactly sure what you mean here. Do you mean when you do a ramaze --create (I haven't used that before, I normally just build by hand for my simple examples)? Can you point me to some documentation?

Thanks again,

Scott
--
Scott
http://steamcode.blogspot.com/

Michael Fellinger

unread,
Jun 5, 2009, 9:59:09 PM6/5/09
to ram...@googlegroups.com
On Sat, Jun 6, 2009 at 9:46 AM, Scott LaBounty<slab...@gmail.com> wrote:
> Pistos,
>
> Thanks, but I'm not exactly sure what you mean here. Do you mean when you do
> a ramaze --create (I haven't used that before, I normally just build by hand
> for my simple examples)? Can you point me to some documentation?

It's from the XHTML helper:
http://github.com/manveru/ramaze/blob/master/lib/ramaze/helper/xhtml.rb
so you can use: #{ js_for(*@page_javascripts) }, and simply push the
ones that you need currently inside (of course you'll need to set
@page_javascripts).

> Thanks again,
>
> Scott
>
> On Fri, Jun 5, 2009 at 7:56 AM, <jesusisramaz...@geoshell.com>
> wrote:
>>
>> 2009/6/5 Scott LaBounty - slab...@gmail.com:
>> > @page_javascript = "page1.js"
>> >
>> > and then in the layout do something like:
>> >
>> > <script src="#@page_javascript" type="text/javascript"></script>
>>
>> I haven't tried it, but that looks like it should work.  But see also
>> the "js" method, as exemplified in the prototype given with "ramaze
>> create".  It's a shortcut to make "<script src....>".
>>
>> --
>> Pistos
>> http://blog.purepistos.net
> --
> Scott
> http://steamcode.blogspot.com/

--
^ manveru

Scott LaBounty

unread,
Jun 5, 2009, 10:28:45 PM6/5/09
to ram...@googlegroups.com
Cool! So to make sure I understand:

Controller:
page_javascripts = Array.new
page_javascripts << "page1_a.js"
page_javascritps << "page1_b.js"

View:
#{js_for(*@page_javascripts)}

Is that correct?

Scott
--
Scott
http://steamcode.blogspot.com/

Scott LaBounty

unread,
Jun 6, 2009, 9:40:04 PM6/6/09
to ram...@googlegroups.com
Hmmm...

I put this:

        @page_javascript = "js/admin.js"

in the controller and this in the layout:

        <?r #{js(@page_javascript)} ?>

It looks like my layout stop processing at that point. I get a blank screen and if I look at the source, I see the line before the <?r code there but nothing following.

Any ideas?

Scott
--
Scott
http://steamcode.blogspot.com/

Michael Fellinger

unread,
Jun 6, 2009, 9:46:37 PM6/6/09
to ram...@googlegroups.com
On Sat, 6 Jun 2009 18:40:04 -0700
Scott LaBounty <slab...@gmail.com> wrote:

> Hmmm...
>
> I put this:
>
> @page_javascript = "js/admin.js"
>
> in the controller and this in the layout:
>
> <?r #{js(@page_javascript)} ?>

#{ js @page_javascript }

the <?r #{ ?> will start a comment, which will mess up the rest of the template
evaluation.

--
^ manveru

Scott LaBounty

unread,
Jun 6, 2009, 10:41:06 PM6/6/09
to ram...@googlegroups.com
Michael,

I'm not sure I understand why what I had earlier would start a comment. If you have a moment, perhaps you could explain a bit more (I'm probably missing something obvious).

Anyway ... now I get:

NoMethodError at /

undefined method `js' for #<MainController:0x9242b70>

Ruby /home/slabounty/RamazeTutorial/Poll/layout/page.xhtml: in binding, line 17
Web GET localhost/

and the following stack trace:

NoMethodError: undefined method `js' for #<MainController:0x9242b70>
    /home/slabounty/RamazeTutorial/Poll/layout/page.xhtml:17:in `binding'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/view/etanni.rb:30:in `eval'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/view/etanni.rb:30:in `result'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/view/etanni.rb:6:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/action.rb:89:in `block (2 levels) in render'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/action.rb:97:in `wrap_in_layout'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/action.rb:89:in `block in render'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/helper/aspect.rb:36:in `aspect_wrap'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/helper/aspect.rb:87:in `wrap_action_call'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/action.rb:84:in `render'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/action.rb:35:in `block in call'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/action.rb:116:in `wrap_in_current'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/action.rb:35:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/action.rb:107:in `wrap_in_layout'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/action.rb:89:in `block in render'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/helper/aspect.rb:36:in `aspect_wrap'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/helper/aspect.rb:87:in `wrap_action_call'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/action.rb:84:in `render'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/action.rb:35:in `block in call'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/action.rb:116:in `wrap_in_current'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/action.rb:35:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/node.rb:295:in `block (2 levels) in action_found'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/node.rb:295:in `catch'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/node.rb:295:in `block in action_found'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/node.rb:295:in `catch'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/node.rb:295:in `action_found'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/node.rb:279:in `try_resolve'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/node.rb:264:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/urlmap.rb:46:in `block in call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/urlmap.rb:40:in `each'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/urlmap.rb:40:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/dynamap.rb:40:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/route.rb:75:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/cascade.rb:19:in `block in call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/cascade.rb:17:in `each'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/cascade.rb:17:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/current.rb:22:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/cascade.rb:19:in `block in call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/cascade.rb:17:in `each'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/cascade.rb:17:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/ramaze-2009.06.04/lib/ramaze/app.rb:93:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/urlmap.rb:46:in `block in call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/urlmap.rb:40:in `each'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/urlmap.rb:40:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/dynamap.rb:40:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/ramaze-2009.06.04/lib/ramaze/reloader.rb:88:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/head.rb:9:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/ramaze-2009.06.04/lib/vendor/etag.rb:11:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/conditionalget.rb:25:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/content_length.rb:13:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/ramaze-2009.06.04/lib/vendor/route_exceptions.rb:22:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/showstatus.rb:20:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/showexceptions.rb:24:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/commonlogger.rb:20:in `_call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/commonlogger.rb:13:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/lint.rb:47:in `_call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/lint.rb:35:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/innate-2009.06/lib/innate/middleware_compiler.rb:52:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/content_length.rb:13:in `call'
    /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/handler/webrick.rb:46:in `service'
    /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
    /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
    /usr/local/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

Thanks again for the help.

Scott
--
Scott
http://steamcode.blogspot.com/

Michael Fellinger

unread,
Jun 6, 2009, 11:43:39 PM6/6/09
to ram...@googlegroups.com
On Sun, Jun 7, 2009 at 11:41 AM, Scott LaBounty<slab...@gmail.com> wrote:
> Michael,
>
> I'm not sure I understand why what I had earlier would start a comment. If
> you have a moment, perhaps you could explain a bit more (I'm probably
> missing something obvious).

<?r ?> is transformed via:
.gsub!(/<\?r\s+(.*?)\s+\?>/m, "#{STOP} \\1; #{ADD} #{START}")


e = Etanni.new('hi there <?r #{foo} ?> bar')
puts e.compile

Will result in:

_out_ =
<<E69t116A65n110N78i105S83e101P80a97R82a97T84o111R82.chomp
hi there
E69t116A65n110N78i105S83e101P80a97R82a97T84o111R82
#{foo}; _out_ <<
<<E69t116A65n110N78i105S83e101P80a97R82a97T84o111R82.chomp
bar
E69t116A65n110N78i105S83e101P80a97R82a97T84o111R82
_out_

As you can see, the #{foo} simply starts a commented line, so the
following heredoc is dropped, if there are no more <?r ?> afterward,
nothing from what follows will be show anymore. As a general rule,
don't use comments inside the ruby processing instructions. You want
output, so use proper string interpolation:

e = Etanni.new('hi there #{foo} bar')
puts e.compile

Will result in:

_out_ =
<<E69t116A65n110N78i105S83e101P80a97R82a97T84o111R82.chomp
hi there #{foo} bar
E69t116A65n110N78i105S83e101P80a97R82a97T84o111R82
_out_

And "hi there #{foo} bar" will properly interpolate in the following eval.
Etanni is a very simple templating engine, if you don't understand
what i just wrote, read the source (all 4 lines of it), and learn, or
stop using this undocumented, obscure, and hardly used engine, and use
something like ERB which gives you <%# %> to make comments, <%= %> for
interpolation, and <% %> for ruby, no confusing interpolation.

> Anyway ... now I get:
>
> NoMethodError at /
>
> undefined method `js' for #<MainController:0x9242b70>

Because it's a helper, you have to use helper(:xhtml) in your
controller before using it.

--
^ manveru

Scott LaBounty

unread,
Jun 7, 2009, 6:59:09 PM6/7/09
to ram...@googlegroups.com
Michael,

OK, I don't understand everything you wrote, but I did clue in as to why what I was trying to do wouldn't work.

Putting in the helper(:xhtml) made everything work fine.

I suppose I could start using ERB (or one of the others), but one of the things that moved me to Ramaze in the first place was the simplicity. For what I'm trying to accomplish (learning a bit about web programming, sharing with others what I've learned), Ramaze is pretty much perfect. I'm not sure I need a bigger templating engine, probably just need to learn the four lines of code so that I understand what's in there better.

Thanks for taking the time to help,

Scott
--
Scott
http://steamcode.blogspot.com/
Reply all
Reply to author
Forward
0 new messages