[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Jonas Hartmann

9/13/2006 7:49:00 PM

whats eRBs <%= doing in contrast to <%?

10 Answers

Sean T Allen

9/13/2006 7:53:00 PM

0

Jonas Hartmann wrote:
> whats eRBs <%= doing in contrast to <%?
>
>
= will output the result.

A. S. Bradbury

9/13/2006 7:53:00 PM

0

On Wednesday 13 September 2006 20:49, Jonas Hartmann wrote:
> whats eRBs <%= doing in contrast to <%?

Check out the documentation - http://www.ruby-doc.org/core/classe...

> <% Ruby code -- inline with output %>
> <%= Ruby expression -- replace with result %>

Alex

Gustav - Railist

9/13/2006 7:55:00 PM

0

Jonas Hartmann wrote:
> whats eRBs <%= doing in contrast to <%?
>
>
>
the evaluated contents of
<%= %> (e.g. <%= "hello" + @user.name%> )
gets inserted into the rendered html file,
whereas
<%%> (e.g. <%if @user.logged_in?%> Welcome Back! <%else%> Sign up! <%end%> )
doesn't go into the eventual html file that gets served to the client...

Gustav Paul
gustav@rails.co.za
itsdEx.com

--
about me:
My greatest achievement was when all the other
kids just learnt to count from 1 to 10,
i was counting (0..9)

- gustav.paul


Jonas Hartmann

9/13/2006 8:13:00 PM

0

A. S. Bradbury wrote:
> On Wednesday 13 September 2006 20:49, Jonas Hartmann wrote:
>> whats eRBs <%= doing in contrast to <%?
>
> Check out the documentation - http://www.ruby-doc.org/core/classe...
>
>> <% Ruby code -- inline with output %>
>> <%= Ruby expression -- replace with result %>
>
> Alex

great stuff, i am not so familiar with the ruby docs they confuse me
sometimes - sry.

Jacob Fugal

9/13/2006 8:13:00 PM

0

On 9/13/06, Jason Roelofs <jameskilton@gmail.com> wrote:
> <% puts "Can't see me" %> --> nothing
>
> <%= puts "Can't see me" %> --> Can't see me

That's wrong. When rendering an ERB template, those two constructs
would be essentially equivalent. The both do the same thing: print
"Can't see me" to STDOUT and add nothing to the rendered output.

Using puts inside ERB does not put things into the rendered output --
it's not equivalent to PHP's echo/print. You should never need to use
puts or print within ERB (unless your intent is to print diagnostics
out to STDOUT while the ERB is being rendered).

<% %> and <%= %> both evaluate an expression. The difference is that
<%= %> includes the result of the expression (cast to a string using
to_s) in the rendered output. The result of the expression:

puts "Can't see me"

is nil, since puts always returns nil. nil.to_s is an empty string, so
even with the <%= %> form, nothing is added to the rendered output.

What you want to do instead is just leave off the puts:

Nothing added: <% "Can't see me" %>
But printed here: <%= "Can see me" %>

Jacob Fugal

Rob Sanheim

9/14/2006 1:06:00 AM

0

On 9/13/06, Jacob Fugal <lukfugl@gmail.com> wrote:
> On 9/13/06, Jason Roelofs <jameskilton@gmail.com> wrote:
> > <% puts "Can't see me" %> --> nothing
> >
> > <%= puts "Can't see me" %> --> Can't see me
>
> That's wrong. When rendering an ERB template, those two constructs
> would be essentially equivalent. The both do the same thing: print
> "Can't see me" to STDOUT and add nothing to the rendered output.
>
> Using puts inside ERB does not put things into the rendered output --
> it's not equivalent to PHP's echo/print. You should never need to use
> puts or print within ERB (unless your intent is to print diagnostics
> out to STDOUT while the ERB is being rendered).
>
> <% %> and <%= %> both evaluate an expression. The difference is that
> <%= %> includes the result of the expression (cast to a string using
> to_s) in the rendered output. The result of the expression:
>
> puts "Can't see me"
>
> is nil, since puts always returns nil. nil.to_s is an empty string, so
> even with the <%= %> form, nothing is added to the rendered output.
>
> What you want to do instead is just leave off the puts:
>
> Nothing added: <% "Can't see me" %>
> But printed here: <%= "Can see me" %>
>
> Jacob Fugal
>
>

One thing I've often wondered is how do you output from w/i a "non
output" block...ie in java scriptlets these are equivalent:

<% out.println("hi") %>
<%= "hi" %>

With ERB I've had some (admittedly rare) cases where this would be
useful. Is it possible?

- rob
--
http://www.robs...
http://www.seekin...
http://www.a...

Ezra Zygmuntowicz

9/14/2006 1:29:00 AM

0


On Sep 13, 2006, at 6:05 PM, Rob Sanheim wrote:

> On 9/13/06, Jacob Fugal <lukfugl@gmail.com> wrote:
>> On 9/13/06, Jason Roelofs <jameskilton@gmail.com> wrote:
>> > <% puts "Can't see me" %> --> nothing
>> >
>> > <%= puts "Can't see me" %> --> Can't see me
>>
>> That's wrong. When rendering an ERB template, those two constructs
>> would be essentially equivalent. The both do the same thing: print
>> "Can't see me" to STDOUT and add nothing to the rendered output.
>>
>> Using puts inside ERB does not put things into the rendered output --
>> it's not equivalent to PHP's echo/print. You should never need to use
>> puts or print within ERB (unless your intent is to print diagnostics
>> out to STDOUT while the ERB is being rendered).
>>
>> <% %> and <%= %> both evaluate an expression. The difference is that
>> <%= %> includes the result of the expression (cast to a string using
>> to_s) in the rendered output. The result of the expression:
>>
>> puts "Can't see me"
>>
>> is nil, since puts always returns nil. nil.to_s is an empty
>> string, so
>> even with the <%= %> form, nothing is added to the rendered output.
>>
>> What you want to do instead is just leave off the puts:
>>
>> Nothing added: <% "Can't see me" %>
>> But printed here: <%= "Can see me" %>
>>
>> Jacob Fugal
>>
>>
>
> One thing I've often wondered is how do you output from w/i a "non
> output" block...ie in java scriptlets these are equivalent:
>
> <% out.println("hi") %>
> <%= "hi" %>
>
> With ERB I've had some (admittedly rare) cases where this would be
> useful. Is it possible?
>
> - rob

<% _erbout << "hi" %>

Its ugly but it works.

-Ezra




Jacob Fugal

9/14/2006 2:32:00 AM

0

On 9/13/06, Ezra Zygmuntowicz <ezmobius@gmail.com> wrote:
> On Sep 13, 2006, at 6:05 PM, Rob Sanheim wrote:
> > One thing I've often wondered is how do you output from w/i a "non
> > output" block...ie in java scriptlets these are equivalent:
> >
> > <% out.println("hi") %>
> > <%= "hi" %>
> >
> > With ERB I've had some (admittedly rare) cases where this would be
> > useful. Is it possible?
>
> <% _erbout << "hi" %>
>
> Its ugly but it works.

You can of course also wrap it in a helper[1] method of your own as
well, to get rid of the ugly. For example:

def echo(value)
_erbout << value.to_s
end

then later in your template:

<% echo "Hi!" %>

Jacob Fugal

[1] If you take "helper" there literally in the rails sense, you
probably want it in your application_helper.rb file; the same can
apply for any ERB usage using "helper" in a more general sense,
however.

Ezra Zygmuntowicz

9/14/2006 3:33:00 AM

0


On Sep 13, 2006, at 7:32 PM, Jacob Fugal wrote:

> On 9/13/06, Ezra Zygmuntowicz <ezmobius@gmail.com> wrote:
>> On Sep 13, 2006, at 6:05 PM, Rob Sanheim wrote:
>> > One thing I've often wondered is how do you output from w/i a "non
>> > output" block...ie in java scriptlets these are equivalent:
>> >
>> > <% out.println("hi") %>
>> > <%= "hi" %>
>> >
>> > With ERB I've had some (admittedly rare) cases where this would be
>> > useful. Is it possible?
>>
>> <% _erbout << "hi" %>
>>
>> Its ugly but it works.
>
> You can of course also wrap it in a helper[1] method of your own as
> well, to get rid of the ugly. For example:
>
> def echo(value)
> _erbout << value.to_s
> end
>
> then later in your template:
>
> <% echo "Hi!" %>
>
> Jacob Fugal
>
> [1] If you take "helper" there literally in the rails sense, you
> probably want it in your application_helper.rb file; the same can
> apply for any ERB usage using "helper" in a more general sense,
> however.
>

Actually that doesn't work in a helper. You will get this error:"

undefined local variable or method `_erbout' for #<#<Class:0x358b7c8>:
0x358b7a0>

its because when you call _erbout it takes a second arg that defaults
to binding. The binding and _erbout are not available within a
helper method.

-Ezra


Jacob Fugal

9/14/2006 3:38:00 AM

0

On 9/13/06, Ezra Zygmuntowicz <ezmobius@gmail.com> wrote:
> Actually that doesn't work in a helper. You will get this error:"
>
> undefined local variable or method `_erbout' for #<#<Class:0x358b7c8>:
> 0x358b7a0>
>
> its because when you call _erbout it takes a second arg that defaults
> to binding. The binding and _erbout are not available within a
> helper method.

Good point, thanks for the pointer. So looks like you're stuck with
the uglies if you really want to do it.

Another option (maybe?) would be to just switch context briefly:

<%
# doing stuff here
# but now I want to print something
%><%= value %><%
# continue doing stuff...
%>

Jacob Fugal