[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

eRuby for Windows

Gavin Kistner

10/13/2004 3:17:00 AM

I have a situation where I need to create/write static web pages using
some non-trivial logic.
(This is separate from a web server.)

The PageTemplate project[1] looked cool, but the simple syntax doesn't
account for the kinds of tests I need to do.

(In short it's:

HTML chunk
foo.each{ |bar|
HTML chunk
}
HTML chunk
foo.each{ |bar|
const.each{ |jim|
conditional output based on bar.bork?(jim)
}
a bunch of conditional output based on return values
of methods on bar, including special processing of that content
}

Right now it's working, but it's a series of nasty:

file << %Q{ ... }
and
file << <<-ENDHTMLCHUNK

type constructs. It's horrific to look at, and certainly not easy to
figure out what's going on for the ancillary ruby-ignorant developers
who may want to tweak the HTML.

eRuby seemed like the ideal solution...but the solution needs to run on
Windows, and I can't seem to find a windows binary for eRuby.

1) Is there an eRuby binary for Windows? (The modruby website says no,
but perhaps it's outdated.) [This also needs to run 'natively' on
Windows...a cygwin type solution is no good, even if eRuby works
there.]

2) Is there a simple ruby-based eRuby-type template system, that lets
me intersperse arbitrary ruby code inside and around the HTML chunks I
want to output?


[1] http://coolnamehere.com/products/pag...
--
(-, /\ \/ / /\/



14 Answers

David Heinemeier Hansson

10/13/2004 9:22:00 AM

0

> 2) Is there a simple ruby-based eRuby-type template system, that lets
> me intersperse arbitrary ruby code inside and around the HTML chunks I
> want to output?

Yes, it's called ERb and is basically a Ruby-version of eRuby. It's
even included in the standard library with Ruby 1.8.x. Have a look at
http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/...
--
David Heinemeier Hansson,
http://www.rubyon... -- Web-application framework for Ruby
http://www.in... -- A No-Step-Three Wiki in Ruby
http://macro... -- TextMate: Code and markup editor for OS
X
http://www.basec... -- Web-based Project Management
http://www.loudthi... -- Broadcasting Brain
http://www.next... -- Development & Consulting Services



Gavin Kistner

10/13/2004 1:21:00 PM

0

On Oct 13, 2004, at 3:22 AM, David Heinemeier Hansson wrote:
> Yes, it's called ERb and is basically a Ruby-version of eRuby. It's
> even included in the standard library with Ruby 1.8.x. Have a look at
> http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/...

Awesome. Thanks, David!

Except...erm...there's no documentation.

When I have more free time I'd love to help out the ruby-doc project
and document this library. But right now I'm working 12 hour days and
don't have time to dive into the source code to figure it out.

Anyone have a bit of an example showing how to set up an HTML template
with embedded ruby code, and then load that template into the program
code at a current state (so all the necessary variables are setup) and
'flatten' the template into the output HTML?



Thomas Counsell

10/13/2004 1:27:00 PM

0

Here is a template:
<html>
<body>
<% if welcoming %>
<h1>Hello <%= name %></h1>
<% else %>
<h1>Goodbye <%= name %></h1>
<% end %>
</body>
</html>

And some code:
require 'erb'
erb = ERB.new( IO.readlines( "template_file_name").join )
welcome = true
name = "Ruby"
html = erb.result( binding )

Hope that gives you an idea.

Tom

On 13 Oct 2004, at 14:21, Gavin Kistner wrote:

> On Oct 13, 2004, at 3:22 AM, David Heinemeier Hansson wrote:
>> Yes, it's called ERb and is basically a Ruby-version of eRuby. It's
>> even included in the standard library with Ruby 1.8.x. Have a look at
>> http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/...
>
> Awesome. Thanks, David!
>
> Except...erm...there's no documentation.
>
> When I have more free time I'd love to help out the ruby-doc project
> and document this library. But right now I'm working 12 hour days and
> don't have time to dive into the source code to figure it out.
>
> Anyone have a bit of an example showing how to set up an HTML template
> with embedded ruby code, and then load that template into the program
> code at a current state (so all the necessary variables are setup) and
> 'flatten' the template into the output HTML?
>
>



Gavin Kistner

10/13/2004 1:32:00 PM

0

On Oct 13, 2004, at 7:27 AM, Thomas Counsell wrote:
> Here is a template:
> <html>
> <body>
> <% if welcoming %>
> <h1>Hello <%= name %></h1>
> <% else %>
> <h1>Goodbye <%= name %></h1>
> <% end %>
> </body>
> </html>
>
> And some code:
> require 'erb'
> erb = ERB.new( IO.readlines( "template_file_name").join )
> welcome = true
> name = "Ruby"
> html = erb.result( binding )

Awesome. Thanks!



Thomas Leitner

10/13/2004 1:36:00 PM

0

On Wed, 13 Oct 2004 22:21:27 +0900
Gavin Kistner <gavin@refinery.com> wrote:

| On Oct 13, 2004, at 3:22 AM, David Heinemeier Hansson wrote:
| > Yes, it's called ERb and is basically a Ruby-version of eRuby. It's
| > even included in the standard library with Ruby 1.8.x. Have a look
| > at http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/...
|
| Awesome. Thanks, David!
|
| Except...erm...there's no documentation.
|
| When I have more free time I'd love to help out the ruby-doc project
| and document this library. But right now I'm working 12 hour days and
| don't have time to dive into the source code to figure it out.
|
| Anyone have a bit of an example showing how to set up an HTML template
|
| with embedded ruby code, and then load that template into the program
| code at a current state (so all the necessary variables are setup) and
|
| 'flatten' the template into the output HTML?
|

I don't know if the following helps, but I'm currently using ERb for one of my projects. The template does not use HTML but LaTeX, but it is the same for HTML anyway.

--------------------- template snippet with ERb code fragments -----

\ecvlastname{<%= value('header/name/surname') %>}
\ecvfirstname{<%= value('header/name/firstname') %>}
\ecvaddress{<%= value('header/address') %>}
<% if resume.elements['header/contact/phone'] %>
\ecvtelephone{<%= value('header/contact/phone') %>}
<% end %>
<% if resume.elements['header/contact/fax'] %>
\ecvfax{<%= value('header/contact/fax') %>}
<% end %>
<% if resume.elements['header/contact/email'] %>
\ecvemail{\texttt{<%= value('header/contact/email') %>}}
<% end %>
\ecvnationality{Austrian}
\ecvdateofbirth{<%= value('header/birth/date') %>}

---------------------------------------------------------------------

---------------------------- Converter class using ERb --------------
class Converter

attr_writer :template
attr_writer :source
attr_writer :config

def initialize
@template = nil
@source = nil
@config = nil
end

def convert
@processor = ResumeProcessor.new( @config )

# Create a new ERB object with the given template
erb = ERB.new( File.read( @template ) )
file = File.new( @source )
@resume = REXML::Document.new( File.new( @source ) ).root
resume = @resume

# Evaluate the template in the current context
print erb.run( binding )
end

def value( path, element = @resume )
@processor.get_value( element.elements[path] )
end

end

c = Converter.new
#... set template, source and config for converter
c.convert
---------------------------------------------------------------------

The @template variable for the Converter class is loaded with the filename of the template. You only have to call the erb.run method (and probably supply a binding) and it will return the result.

Thomas

James Gray

10/13/2004 1:49:00 PM

0

On Oct 13, 2004, at 8:21 AM, Gavin Kistner wrote:

> On Oct 13, 2004, at 3:22 AM, David Heinemeier Hansson wrote:
>> Yes, it's called ERb and is basically a Ruby-version of eRuby. It's
>> even included in the standard library with Ruby 1.8.x. Have a look at
>> http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/...
>
> Awesome. Thanks, David!
>
> Except...erm...there's no documentation.

Looks like you got your answer, but just FYI, ERb also gets some screen
time in the Pickaxe II.

James Edward Gray II



Bill Atkins

10/13/2004 3:32:00 PM

0

http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/...


On Wed, 13 Oct 2004 22:21:27 +0900, Gavin Kistner <gavin@refinery.com> wrote:
> Except...erm...there's no documentation.


Gavin Kistner

10/13/2004 8:39:00 PM

0

On Oct 13, 2004, at 9:31 AM, Bill Atkins wrote:
> http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/...
>
> On Wed, 13 Oct 2004 22:21:27 +0900, Gavin Kistner <gavin@refinery.com>
> wrote:
>> Except...erm...there's no documentation.

Correct. That's the location where there's no documentation.
(A page listing all methods and classes, without describing what they
do or now to use them, is not helpful.)
--
(-, /\ \/ / /\/



dominic sisneros

10/16/2004 2:50:00 PM

0

This site probably has the best documentation.

http://www2a.biglobe.ne.jp/~seki/rub...

Also, the code generation in Ruby uses Erb and has examples


On Oct 13, 2004, at 7:39 AM, Thomas Leitner wrote:

> On Wed, 13 Oct 2004 22:21:27 +0900
> Gavin Kistner <gavin@refinery.com> wrote:
>
> | On Oct 13, 2004, at 3:22 AM, David Heinemeier Hansson wrote:
> | > Yes, it's called ERb and is basically a Ruby-version of eRuby. It's
> | > even included in the standard library with Ruby 1.8.x. Have a look
> | > at http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/...
> |
> | Awesome. Thanks, David!
> |
> | Except...erm...there's no documentation.
> |
> | When I have more free time I'd love to help out the ruby-doc project
> | and document this library. But right now I'm working 12 hour days and
> | don't have time to dive into the source code to figure it out.
> |
> | Anyone have a bit of an example showing how to set up an HTML
> template
> |
> | with embedded ruby code, and then load that template into the program
> | code at a current state (so all the necessary variables are setup)
> and
> |
> | 'flatten' the template into the output HTML?
> |
>
> I don't know if the following helps, but I'm currently using ERb for
> one of my projects. The template does not use HTML but LaTeX, but it
> is the same for HTML anyway.
>
> --------------------- template snippet with ERb code fragments -----
>
> \ecvlastname{<%= value('header/name/surname') %>}
> \ecvfirstname{<%= value('header/name/firstname') %>}
> \ecvaddress{<%= value('header/address') %>}
> <% if resume.elements['header/contact/phone'] %>
> \ecvtelephone{<%= value('header/contact/phone') %>}
> <% end %>
> <% if resume.elements['header/contact/fax'] %>
> \ecvfax{<%= value('header/contact/fax') %>}
> <% end %>
> <% if resume.elements['header/contact/email'] %>
> \ecvemail{\texttt{<%= value('header/contact/email') %>}}
> <% end %>
> \ecvnationality{Austrian}
> \ecvdateofbirth{<%= value('header/birth/date') %>}
>
> ---------------------------------------------------------------------
>
> ---------------------------- Converter class using ERb --------------
> class Converter
>
> attr_writer :template
> attr_writer :source
> attr_writer :config
>
> def initialize
> @template = nil
> @source = nil
> @config = nil
> end
>
> def convert
> @processor = ResumeProcessor.new( @config )
>
> # Create a new ERB object with the given template
> erb = ERB.new( File.read( @template ) )
> file = File.new( @source )
> @resume = REXML::Document.new( File.new( @source ) ).root
> resume = @resume
>
> # Evaluate the template in the current context
> print erb.run( binding )
> end
>
> def value( path, element = @resume )
> @processor.get_value( element.elements[path] )
> end
>
> end
>
> c = Converter.new
> #... set template, source and config for converter
> c.convert
> ---------------------------------------------------------------------
>
> The @template variable for the Converter class is loaded with the
> filename of the template. You only have to call the erb.run method
> (and probably supply a binding) and it will return the result.
>
> Thomas
>
>



James Britt

10/21/2004 5:38:00 AM

0

Gavin Kistner wrote:

> On Oct 13, 2004, at 9:31 AM, Bill Atkins wrote:
>
>> http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/...
>>
>> On Wed, 13 Oct 2004 22:21:27 +0900, Gavin Kistner <gavin@refinery.com>
>> wrote:
>>
>>> Except...erm...there's no documentation.
>
>
> Correct. That's the location where there's no documentation.
> (A page listing all methods and classes, without describing what they do
> or now to use them, is not helpful.)

Mostly true. A list of methods at least gives a place to start looking
in the code, or some idea what might be possible. But I get your basic
point. It is quite frustrating.

The API docs on ruby-doc come from the Ruby source code by way of RDoc.
Hence, if the author of a piece of code opted not to document the code
using inline rdoc comments, and no one else has stepped up to take on
the task, then no docs appear.

So, if anyone has docs for this, and cares to offer them to be placed in
the source code, please speak up. Or, if they are not really suitable
to become part of the source, then can be hosted at ruby-doc as
stand-alone documentation.


Thanks,


James