Yeah...so any thoughts on the best practice for how that helper would
look? My attempt looks like this:
def signin_link
t = <<EOT
%li.first
= link_to('Sign in', :controller => 'user', :action => 'sign in')
EOT
Haml::Engine.new(t).render
end
I was using the heredoc in real life because my haml was a bit more
complicated that this example. But, if the haml in that helper gets
any more complex than that, I feel like I might as well just make it
its own partial and skip the whole helper thing and go straight to
just using render :partial in the original :javascript. (Which *is*
what I ended up doing in real life.)
Truth be told, I've only had to go this far once or twice with stuff
like this inside a :javascript filter, but it seems like a good
exercise to think through.
-scottwb
Evgeny wrote:
> A great method, just put that capture haml in a helper instead of inline
> into haml and it's perfect.
>
> On Sun, Nov 30, 2008 at 19:41, scottwb <[EMAIL PROTECTED]> wrote:
>
> >
> > Here's one thing you could do if you are hell bent on having that haml
> > in there and not in a helper. I tried it once or twice and am
> > undecided on whether or not I like it, but check this out:
> >
> > - signin_link = capture_haml do
> > %li.first
> > = link_to('Sign in', :controller => 'user', :action => 'sign in')
> > - signout_link = capture_haml do
> > %li.last
> > = link_to('Sign out', :controller => 'user', :action => 'sign
> > out')
> > :javascript
> > if (document.cookie.indexOf('signed_in') == -1) {
> > document.write('#{signin_link}');
> > } else {
> > document.write('#{signout_link}');
> > }
> >
> > Basically instead of using a helper for your document.write parameter
> > you can capture the output of that haml code that you want to use into
> > a local variable just before your :javascript filter using
> > haml_capture.
> >
> > I've never used it quite like this example, but this tactic has come
> > in handy, for example, when I have a partial that has tons of haml in
> > it and I want to have it conditionally wrap the whole thing in an
> > extra div. (Though I am not convinced it isn't better to move that
> > extra conditional div outside of that partial.)
> >
> > rvr wrote:
> > > thanks very much for the input. i figured i could do that for the
> > > link_to interpolation, but i guess the haml in the js block just won't
> > > work. appreciate the help.
> > >
> > > On Nov 28, 11:05�am, scottwb <[EMAIL PROTECTED]> wrote:
> > > > You can use #{} interpolation in the text under the :javascript
> > > > filter. So, for your example, you can do:
> > > >
> > > > :javascript
> > > > � if (document.cookie.indexOf('signed_in') == -1) {
> > > > � � document.write('<li class="first">#{link_to("Sign in", :controller
> > > > => "user", :action => "sign_in")}</li>');
> > > > � }
> > > >
> > > > I would also suggest using a helper for the contents of your
> > > > document.write so that you could do something like this:
> > > >
> > > > document.write('#{sign_in_link}');
> > > >
> > > > Hope this helps.
> > > > -scottwb
> > > >
> > > > rvr wrote:
> > > > > actually, i'm not trying to run haml and rails in the browser. i
> > > > > understand that the :javascript filter takes only plain text, so this
> > > > > won't work. but this kind of approach works in rhtml. the point is i
> > > > > want to be able to have a js block which haml still processes for
> > haml
> > > > > tags. if there's no way to do it, fine. but it's not as strange as
> > you
> > > > > seem to think.
> > > >
> > > > > in rhtml i can do this:
> > > >
> > > > > <script type="text/javascript>
> > > > > � if (document.cookie.indexOf('signed_in') == -1) {
> > > > > � � document.write('<li class="first"><%= link_to("Sign
> > > > > In", :controller =>
> > > > > "user", :action => "sign_in")%></li>');
> > > > > � }
> > > > > </script>
> > > >
> > > > > this is not trying to do rails in the browser, since rails processes
> > > > > this text and builds the link tag. guess this approach won't work in
> > > > > haml?
> > > >
> > > > > On Nov 26, 1:13 pm, Evgeny <[EMAIL PROTECTED]> wrote:
> > > > > > You would need some kind of JavaScript-HAML library for that ...
> > and bundle
> > > > > > it with Ruby on Javascript as well, to handle the link_to methods
> > you are
> > > > > > trying to call.
> > > > > > This just does not make sense, you are trying to run HAML and Ruby
> > on Rails
> > > > > > in a user's browser.
> > > >
> > > > > > Please, don't.
> > > >
> > > > > > On Wed, Nov 26, 2008 at 18:35, rvr <[EMAIL PROTECTED]> wrote:
> > > >
> > > > > > > how do i handle haml inside inline js so that it will get
> > processed?
> > > > > > > this code works fine except that the markup inside the
> > document.write
> > > > > > > statements is treated as plain text.
> > > >
> > > > > > > :javascript
> > > > > > > if (document.cookie.indexOf('signed_in') == -1) {
> > > > > > > document.write('%li.first= link_to("Sign In", :controller =>
> > > > > > > "user", :action => "sign_in")');
> > > > > > > document.write('%li.last= link_to("Create
> > > > > > > Profile", :controller => "user", :action => "create")');
> > > > > > > } else {
> > > > > > > document.write('%li.first= link_to("My Account", :controller
> > > > > > > => "user", :action => "user")');
> > > > > > > document.write('%li.last= link_to("Sign Out", :controller =>
> > > > > > > "user", :action => "sign_out")');
> > > > > > > }
> > > >
> > > > > > > any help is appreciated.
> > > >
> > > >
> > >
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Haml" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/haml?hl=en
-~----------~----~----~----~------~----~------~--~---