eRuby for Windows

G

Gavin Kistner

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/pagetemplate/
 
D

David Heinemeier Hansson

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/index.html
--
David Heinemeier Hansson,
http://www.rubyonrails.org/ -- Web-application framework for Ruby
http://www.instiki.org/ -- A No-Step-Three Wiki in Ruby
http://macromates.com/ -- TextMate: Code and markup editor for OS
X
http://www.basecamphq.com/ -- Web-based Project Management
http://www.loudthinking.com/ -- Broadcasting Brain
http://www.nextangle.com/ -- Development & Consulting Services
 
G

Gavin Kistner

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/index.html

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?
 
G

Gavin Kistner

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!
 
T

Thomas Leitner

On Wed, 13 Oct 2004 22:21:27 +0900

| 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/index.html
|
| 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
 
J

James Edward Gray II

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
 
D

dominic sisneros

This site probably has the best documentation.

http://www2a.biglobe.ne.jp/~seki/ruby/erb.html

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


On Wed, 13 Oct 2004 22:21:27 +0900

| 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/index.html
|
| 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
 
J

James Britt

Gavin said:
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
 
J

James Edward Gray II

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.

I've been thinking about helping with some of these modules that still
need documentation. Pretend I'm clueless ('cause I usually am) and
outline the process for me, if you would.

Should I just put documentation in plain text files or embed them in a
copy of the ruby source? Who should I send these changes to? Anything
else I should know?

Thanks.

James Edward Gray II
 
G

Gavin Sinclair

On Oct 21, 2004, at 12:37 AM, James Britt wrote:
I've been thinking about helping with some of these modules that still
need documentation. Pretend I'm clueless ('cause I usually am) and
outline the process for me, if you would.
Should I just put documentation in plain text files or embed them in a
copy of the ruby source? Who should I send these changes to? Anything
else I should know?

In most cases, you should use RDoc formatting in the Ruby or C source
code, following the examples of some that are already done. I am an
appropriate person to send the patches to (or just the whole files).
Another useful point of contact is (e-mail address removed) (mailing
list).

You should work against Ruby's CVS, in general the ruby_1_8 branch.
To get started:

cd ~/Projects # That's my directory, you choose yours.

cvs -d :pserver:[email protected]:/src login
(Just hit ENTER when prompted for password)

cvs -z4 -d :pserver:[email protected]:/src co -r ruby_1_8 ruby18

The stuff you want to look at documenting is in ruby18/{ext,lib}.

I welcome specific questions, privately or publicly, and will eagerly
work with you.

Cheers,
Gavin
 
W

WATANABE Hirofumi

Hi,

James Edward Gray II said:
cvs -d :pserver:[email protected]:/src login
(Just hit ENTER when prompted for password)

cvs -z4 -d :pserver:[email protected]:/src co -r ruby_1_8
ruby18

I'm having trouble with that. Help. <laughs>

% cvs -z4 -d :pserver:[email protected]:/src co -r ruby_1_8
ruby18
cvs server: cannot find module `ruby18' - ignored
cvs [checkout aborted]: cannot expand modules

% cvs -z4 -d :pserver:[email protected]:/src co -r ruby_1_8 -d ruby18 ruby
 

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

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top