[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

HTML generation

Philip Mak

9/12/2003 7:58:00 PM

Is there a list of all the different ways of outputting HTML in Ruby
somewhere?

I can think of using the "puts" statement, eRuby, and CGI. Any others?
e.g.:

"puts" statement:

puts "<p>Hello, world! Your name is <b>#{name}</b>.</p>"

eRuby:

<p>Hello, world! Your name is <b><%=name%></b>.</p>

CGI:

cgi.puts { cgi.p { "Hello, world! Your name is #{cgi.b name}." } }

10 Answers

Ryan Pavlik

9/12/2003 8:17:00 PM

0

On Sat, 13 Sep 2003 04:57:46 +0900
Philip Mak <pmak@aaanime.net> wrote:

> Is there a list of all the different ways of outputting HTML in Ruby
> somewhere?
>
> I can think of using the "puts" statement, eRuby, and CGI. Any others?
> e.g.:
>
> "puts" statement:
>
> puts "<p>Hello, world! Your name is <b>#{name}</b>.</p>"
>
> eRuby:
>
> <p>Hello, world! Your name is <b><%=name%></b>.</p>
>
> CGI:
>
> cgi.puts { cgi.p { "Hello, world! Your name is #{cgi.b name}." } }

I''ll shamelessly plug Mephle here. I have an HTML widget set in
there:

colors = ["Red", "Green", "Blue"]

t = Table.new
f = Form.new
f << table

t << Row["Name:", TextBox.new]
t << Row["Age:", TextBox.new]
t << Row["Favorite Color:", OptionList[*colors]]
t << Row["", Submit["Send"]]

html = t.to_s(...)

Outputs nicely-indented HTML, as much as possible, too.

--
Ryan Pavlik <rpav@mephle.com>

"Must use last ounce of strength to stab from beyond the grave." - 8BT

David Corbin

9/13/2003 12:45:00 AM

0

There''s amrita. A great concept, but I''m not too fond of the implementation.

On Friday 12 September 2003 15:57, Philip Mak wrote:
> Is there a list of all the different ways of outputting HTML in Ruby
> somewhere?
>
> I can think of using the "puts" statement, eRuby, and CGI. Any others?
> e.g.:
>
> "puts" statement:
>
> puts "<p>Hello, world! Your name is <b>#{name}</b>.</p>"
>
> eRuby:
>
> <p>Hello, world! Your name is <b><%=name%></b>.</p>
>
> CGI:
>
> cgi.puts { cgi.p { "Hello, world! Your name is #{cgi.b name}." } }

--
David Corbin <dcorbin@machturtle.com>


Gavin Sinclair

9/13/2003 1:21:00 AM

0

On Saturday, September 13, 2003, 10:45:10 AM, David wrote:

> There''s amrita. A great concept, but I''m not too fond of the implementation.

Please avoid top-posting. What do you dislike about the
implementation? I haven''t used it for quite a while, but I understand
that after 1.something it became a lot faster.

Gavin


> On Friday 12 September 2003 15:57, Philip Mak wrote:
>> Is there a list of all the different ways of outputting HTML in Ruby
>> somewhere?
>>
>> I can think of using the "puts" statement, eRuby, and CGI. Any others?
>> e.g.:
>>
>> "puts" statement:
>>
>> puts "<p>Hello, world! Your name is <b>#{name}</b>.</p>"
>>
>> eRuby:
>>
>> <p>Hello, world! Your name is <b><%=name%></b>.</p>
>>
>> CGI:
>>
>> cgi.puts { cgi.p { "Hello, world! Your name is #{cgi.b name}." } }


David Corbin

9/13/2003 2:19:00 AM

0

On Friday 12 September 2003 21:21, Gavin Sinclair wrote:
> On Saturday, September 13, 2003, 10:45:10 AM, David wrote:
> > There''s amrita. A great concept, but I''m not too fond of the
> > implementation.
>
> Please avoid top-posting. What do you dislike about the
> implementation? I haven''t used it for quite a while, but I understand
> that after 1.something it became a lot faster.
>

Sorry.

It just seemed a little hard to do the things I wanted to do (like href
manipulation) and I definately didn''t like the way it automatically deletes
things that are in the data structure (because they''re nil). (That''s what I
remember, but it''s been more than a month...)

I took the concept and "grew my own" that''s a little more flexible and
powerful I think. The "key" of the hash can be an ID, or and ID + Attribute
name, or in theory (though I haven''t done this yet) an XPath expression. On
the right side, there are a number of possible "manipulators", including a
"delete", and "appender", "replacement" (the default I think), a Form object
which populates INPUT elements, etc.

I *intend* to package it up for use by others, but I''m so busy I don''t know
when I''ll get it done. I don''t really want to release something like that
without good doc, examples, and unit tests.

--
David Corbin <dcorbin@machturtle.com>


Alan Davies

9/17/2003 11:32:00 AM

0

Gavin Sinclair wrote:
> Please avoid top-posting.

Why do people get so upset about this. Personally, I see nothing wrong
with it.

Greg McIntyre

9/17/2003 12:34:00 PM

0

Philip Mak <pmak@aaanime.net> wrote:
> Is there a list of all the different ways of outputting HTML in Ruby
> somewhere?
>
> I can think of using the "puts" statement, eRuby, and CGI. Any others?
> e.g.:
>
> "puts" statement:
>
> puts "<p>Hello, world! Your name is <b>#{name}</b>.</p>"
>
> eRuby:
>
> <p>Hello, world! Your name is <b><%=name%></b>.</p>
>
> CGI:
>
> cgi.puts { cgi.p { "Hello, world! Your name is #{cgi.b name}." } }
>

I use something very close to CGI but a bit easier. The problem with
CGI, I find, is that you need to add the blocks together for
sequential elements. e.g.

cgi.p { "blah blah blah" } +
cgi.p { "blah blah blah" }

Which can actually get quite ugly and cause havoc for your code
indentation, especially when you start introducing conditionals and
loops. If possible I prefer printing on the fly, resulting in something
like (simplified implementation alert):

def paragraph
print "<p>"; yield; print "</p>"
end

paragraph { print "blah blah blah" }
paragraph { print "blah blah blah" }

If you don''t want to print to $stdout, you can set $stdout = something
else beforehand or if you need thread safety (I assume setting $stdout
isn''t a very thread-safe thing to do), uglify your code a little mode by
passing an IO object around.

def paragraph(io)
io << "<p>"; yield io; io << "</p>"
end

paragraph(io) { io << "blah blah blah" }
paragraph(io) { io << "blah blah blah" }

(You don''t really need to use the block parameter but I pass it anyway
just in case.)

I find this method works really well. You end up with very neat code
and its indentation level mimics that in the HTML output so its
structure is always clear (and can be automatically re-indented with
standard code indenters). You don''t have to escape strings or
rearrange your code to add iteration or conditionals and that turns
out to be an enormous plus for neatness IMO. In fact, I think this
method is even easier and less error-prone than hand coding HTML
because it guarrantees you close off your open tags! e.g.

table do
tr("class"=>"headings") do
th { print "Name" }
th { print "Age" }
end
for person in people.sort
tr("class"=>"record") do
td { print person.name }
td { print person.age }
end
end
end

And you also come up with context sensitive names instead of "tr" and
"td". You can use Ruby''s alias command or define methods which make
things easier for particular tasks, and factor out common logic.
Putting in another layer also lets you go back and change your
definitions later if you need to, which is nice.

e.g.

def ages(headings)
table({''class''=>''ages''}) do
tr({''class''=>''headings''}) do
headings.each do |i|
th { print i }
end
end
yield
end
end

def record(*values)
tr({''class''=>''record''}) do
values.each do |i|
td { print i }
end
end
end

ages ''Name Age''.split do
for person in people.sort
record person.name, person.age
end
end


Anyway, just my 2c. ;)

--
Greg McIntyre ======[ greg@puyo.cjb.net ]===[ http://pu... ]===

Tom Copeland

9/17/2003 2:34:00 PM

0

On Wed, 2003-09-17 at 07:34, Alan Davies wrote:
> Gavin Sinclair wrote:
> > Please avoid top-posting.
>
> Why do people get so upset about this. Personally, I see nothing wrong
> with it.

A: Because people read from top to bottom.

Q: Why is top posting bad?

Yours,

Tom



Gavin Sinclair

9/17/2003 2:42:00 PM

0

On Wednesday, September 17, 2003, 9:34:45 PM, Alan wrote:

> Gavin Sinclair wrote:
> > Please avoid top-posting.

> Why do people get so upset about this. Personally, I see nothing wrong
> with it.

It''s pretty obvious really; top-posting leaves the thread of
discussion out of order. It is usually preferable to reply to a
specific context (i.e. insert your points among quoted text) than to
reply to an entire message. Even if you want to reply to a whole
message, you don''t know who will reply to you, and top-posting ensures
that subsequent replies, if using context threading, will be out of
order.

Basically, context threading (if that''s the right word), combined with
judicious snipping, helps the cause of clearer communication, which
someone searching the archives in a year''s time will appreciate.

I''m sure there will be heaps of dissertations on this topic on the
''net, both for and against. There are also discussions in the
ruby-talk archives.

Cheers,
Gavin


Gavin Sinclair

9/17/2003 2:43:00 PM

0

On Thursday, September 18, 2003, 12:33:42 AM, Tom wrote:

> On Wed, 2003-09-17 at 07:34, Alan Davies wrote:
>> Gavin Sinclair wrote:
>> > Please avoid top-posting.
>>
>> Why do people get so upset about this. Personally, I see nothing wrong
>> with it.

> A: Because people read from top to bottom.

> Q: Why is top posting bad?


Sigh... one day I will learn succinctness... ;)

Gavin


dagbrown

9/17/2003 3:38:00 PM

0

In article <3f684623$1@primark.com>,
Alan Davies <NOSPAMcs96and@yahoo.co.ukNOSPAM> wrote:
: Gavin Sinclair wrote:
: > Please avoid top-posting.
: Why do people get so upset about this. Personally, I see nothing
: wrong with it.

Top-posting disrupts the flow of discussion--it''s like having a
conversation backwards. Also, the response is not by the thing
you''re responding to, so it''s unclear what you''re replying to.
Also, it encourages leaving the entire original article in place,
which wastes bandwidth (by which I mean, bandwidth in my brain;
Internet bandwidth cost is negligible).

--Dave