Can we have these two added to ERb?

  • Thread starter SpringFlowers AutumnMoon
  • Start date
S

SpringFlowers AutumnMoon

I like Ruby tremendously...

and currently having some headache with the "$" and ";" in PHP, like

$i = $j;

in every single line, vs Ruby's simplicity

i = j

maybe the $i is so that outputting the variable is easy in

print "the value is $i"
in ERb, we need to write

"the value is #{i}"

and it is a bit more typing. can we have something like

"the value is ##i" as a shorthand? and use #{ } when it involves more
complicated expressions?

The other thing is that in PHP, they use "print" and "echo" to output
the HTML code. In Ruby, we always use <%= evaluated_value %> and it
might have a limit of always breaking up the code into <% %> and <%=
%> and <% %>

<% results.each do |k,v| %>
<%= "#{k} #{v}" %>
<% end %>

the people who like to use inject may be happy because they can have it
all in one line <%= results.inject {|k,v| k += ... %>

but if we can have some way that the STDOUT is made into HTML code, like

<%p results.each {|k,v| printf "... " %>

or

<%p

code

%>


then it can give us some ease when templating.
 
E

Eric Hodel

I like Ruby tremendously...

and currently having some headache with the "$" and ";" in PHP, like

$i = $j;

in every single line, vs Ruby's simplicity

i = j

maybe the $i is so that outputting the variable is easy in

print "the value is $i"
in ERb, we need to write

"the value is #{i}"

and it is a bit more typing. can we have something like

"the value is ##i" as a shorthand? and use #{ } when it involves more
complicated expressions?

There is a shortcut for ivars, cvars and globals, but I find it too
evil to write down. Especially since it can lead to mysterious
syntax errors when you're not paying attention. I'm sure you'll find
it soon enough.
The other thing is that in PHP, they use "print" and "echo" to output
the HTML code. In Ruby, we always use <%= evaluated_value %>
and it
might have a limit of always breaking up the code into <% %> and
<%=
%> and <% %>

<% results.each do |k,v| %>
<%= "#{k} #{v}" %>
<% end %>

the people who like to use inject may be happy because they can
have it
all in one line <%= results.inject {|k,v| k += ... %>

but if we can have some way that the STDOUT is made into HTML code,
like

This should be easy enough to do by overwriting $stdout with an
object that writes to _erbout (or whatever it is) while in an erb
template.
 
P

Phrogz

in ERb, we need to write

"the value is #{i}"

and it is a bit more typing. can we have something like

"the value is ##i" as a shorthand? and use #{ } when it involves more
complicated expressions?

This feature has nothing to do with ERb; it's part of Ruby's string
interpolation.

For global and instance variables, you can already do this:
"the value is #@i"

If that's not enough, you'll have to make a case for an alternate
syntax for local variable interpolation.

The other thing is that in PHP, they use "print" and "echo" to output
the HTML code. In Ruby, we always use <%= evaluated_value %> and it
might have a limit of always breaking up the code into <% %> and <%=
%> and <% %>

<% results.each do |k,v| %>
<%= "#{k} #{v}" %>
<% end %>

the people who like to use inject may be happy because they can have it
all in one line <%= results.inject {|k,v| k += ... %>

but if we can have some way that the STDOUT is made into HTML code, like

<%p results.each {|k,v| printf "... " %>


1) ERb has a local variable that you can append to if you like.

C:\>qri ERB.new
---------------------------------------------------------------
ERB::new
ERB::new(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout')
------------------------------------------------------------------------
Constructs a new ERB object with the template specified in _str_.

...

_eoutvar_ can be used to set the name of the variable ERB will
build up its output in. This is useful when you need to run
multiple ERB templates through the same binding and/or when you
want to control where output ends up. Pass the name of the
variable
to be used inside a String.

I find this useful when I want to control whitespace exactly and don't
want to have to mess up my <%...%> delimiters to do so. For example:

<%10.times{
_erbout << "\t\tHello World!\n"
}%>


2) After prompting for this a couple years ago, people on the list
helped create a patch to ERB to allow print and puts to concatenate to
this string. See [1] below. This was followed by some discussion
(again prompted by me) for a RCR to allow this to work. See [2] below.
The discussion there is probably worth reading before we dive into
this again.

[1] http://blade.nagaokaut.ac.jp/cgi-bi...talk/155537?155418-156804+split-mode-vertical

[2] http://blade.nagaokaut.ac.jp/cgi-bi...talk/152709?152641-153992+split-mode-vertical
 
P

Phrogz

2) After prompting for this a couple years ago, people on the list
helped create a patch to ERB to allow print and puts to concatenate to
this string. [...]

I forgot to mention - this patch is available for Ruby 1.8.2 at:
ftp://phrogz.net/rubylibs/erb_1.8.2_with_puts.rb.gz

I'm not sure how much (if any) ERb has changed between 1.8.2 and
1.8.6, but it should be pretty easy to carry this patch forward to the
current version and patch your own ERb if you want this.
 
S

SpringFlowers AutumnMoon

Gavin said:
This feature has nothing to do with ERb; it's part of Ruby's string
interpolation.

For global and instance variables, you can already do this:
"the value is #@i"

If that's not enough, you'll have to make a case for an alternate
syntax for local variable interpolation.

how about the case that in RoR or server side scripting usage, it can be
quite simpler, to contend for Ruby as a server side scripting language?
 
S

SpringFlowers AutumnMoon

Eric said:
This should be easy enough to do by overwriting $stdout with an
object that writes to _erbout (or whatever it is) while in an erb
template.

hm, that's interesting, the program "erb" can already take

puts "string"

and output it...

but in a somewhat funny order:


<%= "haha" %>

<% puts "so how about this?" %>

C:\rails\depot>erb try_erb.txt
so how about this?

haha

However, when I use puts "string" in index.rhtml on RoR, it won't get
displayed. So RoR actually uses eRuby but not erb, and both are just
called "Embedded Ruby" as "ERb"?

So my original question becomes, can we have those two features added to
eRuby. (for RoR and for server side scripting language)
 
K

Konrad Meyer

--nextPart2375444.YyQukg1AuU
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Quoth SpringFlowers AutumnMoon:
I like Ruby tremendously...
=20
and currently having some headache with the "$" and ";" in PHP, like
=20
$i =3D $j;
=20
in every single line, vs Ruby's simplicity
=20
i =3D j
=20
maybe the $i is so that outputting the variable is easy in
=20
print "the value is $i"
in ERb, we need to write
=20
"the value is #{i}"
=20
and it is a bit more typing. can we have something like
=20
"the value is ##i" as a shorthand? and use #{ } when it involves more
complicated expressions?
=20
The other thing is that in PHP, they use "print" and "echo" to output
the HTML code. In Ruby, we always use <%=3D evaluated_value %> and it
might have a limit of always breaking up the code into <% %> and <%=3D
%> and <% %>
=20
<% results.each do |k,v| %>
<%=3D "#{k} #{v}" %>
<% end %>
=20
the people who like to use inject may be happy because they can have it
all in one line <%=3D results.inject {|k,v| k +=3D ... %>
=20
but if we can have some way that the STDOUT is made into HTML code, like
=20
<%p results.each {|k,v| printf "... " %>
=20
or
=20
<%p
=20
code
=20
%>
=20
=20
then it can give us some ease when templating.

PHP has <?=3D $var ?> shorthand as well.

=2D-=20
Konrad Meyer <[email protected]> http://konrad.sobertillnoon.com/

--nextPart2375444.YyQukg1AuU
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBG/UmJCHB0oCiR2cwRAkMwAKCVh9LsVzktr7w3s05ISR/WSYyfkwCgv2zr
j99oJPj4wGf2+JVlBA1DvmI=
=BSHe
-----END PGP SIGNATURE-----

--nextPart2375444.YyQukg1AuU--
 
P

Phrogz

hm, that's interesting, the program "erb" can already take

puts "string"

and output it...

No, it can't. The ruby program evaluates that and outputs it
separately. It is not part of the string resulting from evaluating the
ERB template.
but in a somewhat funny order:

<%= "haha" %>

<% puts "so how about this?" %>

C:\rails\depot>erb try_erb.txt
so how about this?

haha

What's happening here is that while evaluating the ERB template, the
#puts method is called...which outputs it immediately. (And as a
result, doesn't happen to change the ERB string at all.) Then, later,
the template finishes, and has just the contents of "haha".

However, when I use puts "string" in index.rhtml on RoR, it won't get
displayed. So RoR actually uses eRuby but not erb, and both are just
called "Embedded Ruby" as "ERb"?

In RoR, you'll see the output of the puts call coming out in your
console or logfile (I think); since the output isn't placed in the ERB
result string, however, it never gets put in the HTML that is sent to
the web browser.
 
P

Phrogz

how about the case that in RoR or server side scripting usage, it can be
quite simpler, to contend for Ruby as a server side scripting language?

You don't need to convince *me*; I'm a fan of the idea. You need to
convince everyone else who feels that it's just as easy (and more
clear) to concatenate to _erbout << instead of print/puts/p.

If you read the full discussion when I proposed the RCR two years ago,
you'll see my arguments and theirs. Don't be disheartened, but if
you're going to make a strong case for it effectively, you'll want to
continue the discussion previously had, instead of starting anew.

(Saying "This feature is important because it will make Ruby more
popular in the world of ____" is nice, but it doesn't say _why_ it's
important to have that feature, or why you think it will make Ruby
more popular. You need to either describe what it makes possible that
was not possible before, or convince people why it's a much better way
to do things than the currently-possible solution.)
 
P

Phrogz

PHP has <?= $var ?> shorthand as well.

As does ERB.

<%= any_expression_including_a_variable %>
e.g.

<%= foo %>
<%= @foo %>
<%= $foo %>

#to_s is called on the expression before throwing it into the string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,337
Members
48,014
Latest member
saradhi

Latest Threads

Top