From: Josh Cheek Date: 2012-07-25T22:42:10+09:00 Subject: Re: String interpolation / hash of local variables --20cf3071c6acfbd94c04c5a7a1ce Content-Type: text/plain; charset=ISO-8859-1 On Tue, Jul 24, 2012 at 10:08 PM, Gerbeck Shark wrote: > Hello all- > > I'm trying to do something likes > > Demonstration Ruby/logic > > ------- > template = <<-HTML >
%{example}
> ... >
%{foobar}
> HTML > > for i in special_list > example = .... > foobar = ... > arr.push template % return_magical_hash_with_local_variables() > ------- > > in Python there is a locals() function that returns a dictionary/hash of > local variables. > > In Ruby I've found local_variables() but that does not provide the > values. It should for just the example above! Or perhaps I can be > enlightened by someone here? > > The other, dirtier idea is something like this: > > ------- > template = %q{ >
#{example}
> ... >
%{foobar}
> } > > for i in special_list > example = .... > foobar = ... > arr.push eval("\"template\"") > ------- > > I believe one should avoid calling eval as much as possible. I tried the > "Facets" package, > "String.interpolate{template}" calls eval, it just masks the fact. > > This could be avoided if there were a function that return a dictionary > or hash of local variables. > > -- > Posted via http://www.ruby-forum.com/. > > There is a templating library in the stdlib called erb ( http://rdoc.info/stdlib/erb/ERB) require 'erb' template = <<-HTML
<%= example %>
<%= foobar %>
HTML # ===== simple example with toplevel locals ===== example = 'EXAMPLE' foobar = 'FOOBAR' ERB.new(template).result binding # => "
EXAMPLE
\n
FOOBAR
\n" # ===== more realistic example with methods on an object ===== # you can technically do ivars in these, too, which # is what most web frameworks use, but that sucks IMO class MyTemplateRenderer attr_accessor :example, :foobar def initialize(example, foobar) self.example, self.foobar = example, foobar end def render(template) ERB.new(template).result binding end end renderer = MyTemplateRenderer.new 'ExAmPlE', 'FoObAr' renderer.render template # => "
ExAmPlE
\n
FoObAr
\n" --20cf3071c6acfbd94c04c5a7a1ce Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On Tue, Jul 24, 2012 at 10:08 PM, Gerbeck Shark = <lists@ruby-forum.com> wrote:
Hello all-

I'm trying to do something likes

Demonstration Ruby/logic

-------
template =3D <<-HTML
=A0<div>%{example}</div>
=A0 ...
=A0<div>%{foobar}</div>
HTML

for i in special_list
=A0 =A0 =A0example =3D ....
=A0 =A0 =A0foobar =3D ...
=A0 =A0 =A0arr.push template % return_magical_hash_with_local_variables() -------

in Python there is a locals() function that returns a dictionary/hash of local variables.

In Ruby I've found local_variables() but that does not provide the
values. It should for just the example above! Or perhaps I can be
enlightened by someone here?

The other, dirtier idea is something like this:

-------
template =3D %q{
=A0<div>#{example}</div>
=A0 ...
=A0<div>%{foobar}</div>
}

for i in special_list
=A0 =A0 =A0example =3D ....
=A0 =A0 =A0foobar =3D ...
=A0 =A0 =A0arr.push eval("\"template\"")
-------

I believe one should avoid calling eval as much as possible. I tried the "Facets" package,
"String.interpolate{template}" calls eval, it just masks the fact= .

This could be avoided if there were a function that return a dictionary
or hash of local variables.

--
Posted via http://= www.ruby-forum.com/.


There is a templating library in = the stdlib called erb (http://r= doc.info/stdlib/erb/ERB)


require 'erb'

template =3D <<-HTML
=A0<div><%=3D example %></div>
=A0<= div><%=3D foobar =A0%></div>
HTML

# =3D=3D=3D=3D=3D simple example with toplevel locals =3D=3D=3D=3D=3D
=
example =3D 'EXAMPLE'
foobar =3D 'FOOBAR'
ERB.new(template).result binding # =3D> " <div>EXAMPL= E</div>\n <div>FOOBAR</div>\n"


# =3D=3D=3D=3D=3D more realistic example= with methods on an object =3D=3D=3D=3D=3D
# you can technically = do ivars in these, too, which
# is what most web frameworks use, = but that sucks IMO
class MyTemplateRenderer
=A0 attr_accessor :example, :foobar=
=A0=A0
=A0 def initialize(example, foobar)
= =A0 =A0 self.example, self.foobar =3D example, foobar
=A0 end
=A0=A0
=A0 def render(template)
=A0 =A0 ERB.new(template).result bi= nding
=A0 end
end

renderer =3D= MyTemplateRenderer.new 'ExAmPlE', 'FoObAr'
rende= rer.render template # =3D> " <div>ExAmPlE</div>\n <d= iv>FoObAr</div>\n"

--20cf3071c6acfbd94c04c5a7a1ce--