Forum: Ruby on Rails <% %> is not exactly the same as <?php ?>

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
389102eb1ab4abf162c1cd1f7b147d5a?d=identicon&s=25 Jian Lin (jianlin)
on 2009-05-09 08:33
it seems that <% %> is not exactly the same as <?php ?>  in which,


PHP's
<?php echo "something" ?>

will add to the output

but

ERB's
<% puts "something" %>

will not?

Does someone know if JSP and ASP behave like ERB or PHP and can make a
summary of their likes and differences?   Thank you.
389102eb1ab4abf162c1cd1f7b147d5a?d=identicon&s=25 Jian Lin (jianlin)
on 2009-05-09 11:33
Jian Lin wrote:
> it seems that <% %> is not exactly the same as <?php ?>  in which,
>
>
> PHP's
> <?php echo "something" ?>
>
> will add to the output
>
> but
>
> ERB's
> <% puts "something" %>
>
> will not?

actually, i found that <% puts 123 %> prints 123 to the stdout.  so the
shell that is running script/server will see "123"... but the 123 is not
part of the webpage generated.  Is there a way or option to include that
123 as part of the generated HTML?  thanks.
Bd4458a2acbf32c9b10311cae76614ff?d=identicon&s=25 Rob Lacey (robl)
on 2009-05-09 11:57
(Received via mailing list)
<%= "blah" %>

is what you are looking for

RobL
http://www.robl.me

2009/5/9 Jian Lin <rails-mailing-list@andreas-s.net>

> > but
>
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>


--
Rob Lacey
contact@robl.me
http://www.robl.me
389102eb1ab4abf162c1cd1f7b147d5a?d=identicon&s=25 Jian Lin (jianlin)
on 2009-05-09 12:07
Rob Lacey wrote:
> <%= "blah" %>
>
> is what you are looking for

actually... sometimes i want to output multiple things inside of <%  %>,
or put everything inside a loop and inside a <%  %>,  so that's why the
question of <% puts 123 %>
D0bb06c137d862bffd9a76500c8d2366?d=identicon&s=25 Alberto Santini (asan)
on 2009-05-09 14:01
Jian Lin wrote:
> actually... sometimes i want to output multiple things inside of <%  %>,
> or put everything inside a loop and inside a <%  %>,  so that's why the
> question of <% puts 123 %>

I think you should use something like:

<% for x in y do %>
  <%= "#{x.z}, #{x.a}" %>
<% end %>
389102eb1ab4abf162c1cd1f7b147d5a?d=identicon&s=25 Jian Lin (jianlin)
on 2009-05-09 14:36
Alberto Santini wrote:
> Jian Lin wrote:
>> actually... sometimes i want to output multiple things inside of <%  %>,
>> or put everything inside a loop and inside a <%  %>,  so that's why the
>> question of <% puts 123 %>
>
> I think you should use something like:
>
> <% for x in y do %>
>   <%= "#{x.z}, #{x.a}" %>
> <% end %>

so for example... if i have the code

<%
begin
  t = ''
  s = Iconv.conv("UTF-32", "UTF-8", some_utf8_string)

  (s.length / 4).times do |i|
    b3 = s[i*4 + 2]
    b4 = s[i*4 + 3]
    t += ("&#x" + "%02X" % b3) + ("%02X" % b4) + ";"
  end
rescue => details
  t = "exception " + details
end
%>

<%= t %>

then if i don't want to concat the output into t first... then would it
be a bit messy to use


<%
begin
  t = ''
  s = Iconv.conv("UTF-32", "UTF-8", some_utf8_string)

  (s.length / 4).times do |i|
    b3 = s[i*4 + 2]
    b4 = s[i*4 + 3]
%>

<%= ("&#x" + "%02X" % b3) + ("%02X" % b4) + ";" %>

<%
  end
rescue => details
%>
<%= "exception " + details %>

<%
end
%>
Eecd295cdec2454c66e2015036a39408?d=identicon&s=25 Scott Nj (scottnj)
on 2009-05-09 14:58
(Received via mailing list)
putting this code into a helper method or partial will keep your views
clean.
http://api.rubyonrails.org/classes/ActionControlle...
http://api.rubyonrails.org/classes/ActionView/Partials.html
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-09 15:05
(Received via mailing list)
I would suggest most of that should be in the controller not the view,
you
don't want to be rescuing exceptions in the view.

Colin

2009/5/9 Jian Lin <rails-mailing-list@andreas-s.net>
This topic is locked and can not be replied to.