[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ERB: import function

Alvaro Bautista

2/12/2008 11:48:00 PM

I want to import rhtml files into another using ERB. I wrote a function:

def import(fn)
e = ""
File.open(fn) do |f|
e = ERB.new(f.read)
end
e.result
end

This function does what I want, reads the template file and processes it
returning the result string.

The problem is that if I do, for instance:

<html>
<body>
Some content
<%= import('some.rhtml') %>
</body>
</html>

The result contains only the some.rhtml processed file and the content
following the import line.
--
Posted via http://www.ruby-....

3 Answers

Ryan Davis

2/13/2008 8:10:00 AM

0


On Feb 12, 2008, at 3:48 PM, Alvaro Bautista wrote:

> def import(fn)
> e = ""
> File.open(fn) do |f|
> e = ERB.new(f.read)
> end
> e.result
> end

I can't help you with your actual problem, but I can suggest how to
simplify the code:

def import path
ERB.new(File.read(path)).result
end

I doubt that'll have any impact on the problem at hand, and it won't
affect the performance by much, but it is entirely comprehensible at a
glance and I think that counts for a lot.


Jano Svitok

2/13/2008 9:49:00 AM

0

On Feb 13, 2008 9:10 AM, Ryan Davis <ryand-ruby@zenspider.com> wrote:
>
> On Feb 12, 2008, at 3:48 PM, Alvaro Bautista wrote:
>
> > def import(fn)
> > e = ""
> > File.open(fn) do |f|
> > e = ERB.new(f.read)
> > end
> > e.result
> > end
>
> I can't help you with your actual problem, but I can suggest how to
> simplify the code:
>
> def import path
> ERB.new(File.read(path)).result
> end
>

This is halfway to solution:

def import path
ERB.new(File.read(path), nil, nil, '_x_').result()
end

Notice the other parameters, especially the last one. ERB uses one
variable to store the output. If you don't specify any,
it will use a default one - and each nested ERB will overwrite the new
one. The solution is 1. use another name for each nested import
2. use another binding for each nested import (i.e. make another
scope, and let ERB create another var with the same name
in the new scope/binding, leaving the ability to return to the previous one)

I'll leave the details for your homework ;-)

http://ruby-doc.org/core/classes/ERB.ht...

Jano

Alvaro Bautista

2/13/2008 11:00:00 AM

0

Jano Svitok wrote:
> This is halfway to solution:
>
> def import path
> ERB.new(File.read(path), nil, nil, '_x_').result()
> end
>
> Notice the other parameters, especially the last one. ERB uses one
> variable to store the output. If you don't specify any,
> it will use a default one - and each nested ERB will overwrite the new
> one. The solution is 1. use another name for each nested import
> 2. use another binding for each nested import (i.e. make another
> scope, and let ERB create another var with the same name
> in the new scope/binding, leaving the ability to return to the previous
> one)

I use the last parameter to put the output in a variable an it works.

Thank you.

--
Posted via http://www.ruby-....