[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

simple eruby -- including files

Chad Perrin

3/29/2007 8:09:00 PM

NOTE: I'll try one more time. For some reason, this message hasn't
been getting through. It occurred to me that the mailing list might be
discarding messages with (X)HTML in them, so I replaced all greater-than
and less-than symbols with the corresponding bracket symbols. Please
pretend, while reading this, that it's all valid XHTML -- and please
forgive me if all three messages ultimately make it through.


I've been playing with eruby a little bit -- a VERY LITTLE bit, mind
you. I haven't gotten far yet.

It seems like an excellent way to finally get away from PHP in certain
types of work I do. The problem is that some of the concepts of PHP
don't seem to translate directly, and the only howtos I'm able to find
for eruby are anemic at best. There's plenty of stuff about how to
install it, and precious little about how to make use of it.

My current frustration is figuring out how to achieve the following:

1. an index.rhtml page that defines some variables

2. a template.whatever page that contains an XHTML page with evaluation
of ruby variables defined in the index.rhtml page to produce
page-specific content

In PHP, it would be something like the following example.

index.php:
[?php
$title = "Home";
require_once('template.php');
?]

template.php:
[html]
[head]
[title]Welcome [?php echo $title ?][/title]
[/head]
[body]
[p]blah blah blah[/p]
[/body]
[/html]

So . . . ideas? How would I go about achieving the same results with
eruby? Unless and until I can figure this out, there's a HUGE roadblock
in my way of the application of the DRY principle.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
"The first rule of magic is simple. Don't waste your time waving your
hands and hopping when a rock or a club will do." - McCloctnick the Lucid

5 Answers

John Joyce

3/29/2007 11:01:00 PM

0

It really does work pretty much the same way as you would do it with
PHP.
You can make it as simple or as complex as you like.
Use Ruby include or require for the same files.
eRuby is just a lot more concise than PHP is the result.
Your pages become readable again.
It's not different in terms of breaking up files into:
Commonly reused elements (header, navigation, footer, analytics scripts)
Content unique to a page.

For a simple, fairly static site, you're biggest concern is this
organization of files.
Anything that is the same on multiple pages should get put into a
separate file.

Of course you can also always just parse files by having the files
written and produced once then storing those, thus saving the
processing time on the server.

On Mar 30, 2007, at 5:09 AM, Chad Perrin wrote:

> NOTE: I'll try one more time. For some reason, this message hasn't
> been getting through. It occurred to me that the mailing list
> might be
> discarding messages with (X)HTML in them, so I replaced all greater-
> than
> and less-than symbols with the corresponding bracket symbols. Please
> pretend, while reading this, that it's all valid XHTML -- and please
> forgive me if all three messages ultimately make it through.
>
>
> I've been playing with eruby a little bit -- a VERY LITTLE bit, mind
> you. I haven't gotten far yet.
>
> It seems like an excellent way to finally get away from PHP in certain
> types of work I do. The problem is that some of the concepts of PHP
> don't seem to translate directly, and the only howtos I'm able to find
> for eruby are anemic at best. There's plenty of stuff about how to
> install it, and precious little about how to make use of it.
>
> My current frustration is figuring out how to achieve the following:
>
> 1. an index.rhtml page that defines some variables
>
> 2. a template.whatever page that contains an XHTML page with
> evaluation
> of ruby variables defined in the index.rhtml page to produce
> page-specific content
>
> In PHP, it would be something like the following example.
>
> index.php:
> [?php
> $title = "Home";
> require_once('template.php');
> ?]
>
> template.php:
> [html]
> [head]
> [title]Welcome [?php echo $title ?][/title]
> [/head]
> [body]
> [p]blah blah blah[/p]
> [/body]
> [/html]
>
> So . . . ideas? How would I go about achieving the same results with
> eruby? Unless and until I can figure this out, there's a HUGE
> roadblock
> in my way of the application of the DRY principle.
>
> --
> CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
> "The first rule of magic is simple. Don't waste your time waving your
> hands and hopping when a rock or a club will do." - McCloctnick the
> Lucid
>


Chad Perrin

3/29/2007 11:40:00 PM

0

On Fri, Mar 30, 2007 at 08:00:52AM +0900, John Joyce wrote:
> It really does work pretty much the same way as you would do it with
> PHP.
> You can make it as simple or as complex as you like.
> Use Ruby include or require for the same files.
> eRuby is just a lot more concise than PHP is the result.
> Your pages become readable again.
> It's not different in terms of breaking up files into:
> Commonly reused elements (header, navigation, footer, analytics scripts)
> Content unique to a page.

Could you provide an example? The following, an apparently direct
translation from PHP (as far as I can tell), doesn't work.

index.rhtml:
<%
title = "Home"
require 'template.rhtml'
%>

template.rhtml:
<html>
<head>
<title>Welcome <%= title %></title>
</head>
<body>
<p>blah blah blah</p>
</body>
</html>


>
> For a simple, fairly static site, you're biggest concern is this
> organization of files.
> Anything that is the same on multiple pages should get put into a
> separate file.

I'm aware of this. That's why I'm trying to figure out how to make the
above tactic work.


>
> Of course you can also always just parse files by having the files
> written and produced once then storing those, thus saving the
> processing time on the server.

Not always an option -- but I have used it in the past.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
Amazon.com interview candidate: "When C++ is your
hammer, everything starts to look like your thumb."

Ezra Zygmuntowicz

3/30/2007 3:29:00 AM

0


On Mar 29, 2007, at 4:40 PM, Chad Perrin wrote:

> Could you provide an example? The following, an apparently direct
> translation from PHP (as far as I can tell), doesn't work.
>
> index.rhtml:
> <%
> title = "Home"
> require 'template.rhtml'
> %>
>
> template.rhtml:
> <html>
> <head>
> <title>Welcome <%= title %></title>
> </head>
> <body>
> <p>blah blah blah</p>
> </body>
> </html>

Chad-

Erb doesn't do anything like that by default. But you can fudge it
pretty easily:

ez work $ cat include.rb
require 'erb'
def _include(template, bind=binding)
erb = ERB.new(IO.read(File.join(File.dirname(__FILE__),template)))
erb.result(bind)
end
ez work $ cat foo.rhtml
<% require 'include' %>
<% 10.times do %>
<%= _include 'bar.rhtml' %>
<% end %>
ez socialpicks $ cat bar.rhtml
<%= "hello world!" %>
ez work $ erb foo.rhtml


hello world!


hello world!


hello world!


hello world!


hello world!


hello world!


hello world!


hello world!


hello world!


hello world!


You can't use 'include' because of Module#include so I used
_include. Have fun!


Cheers
-- Ezra Zygmuntowicz
-- Lead Rails Evangelist
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)



Chad Perrin

3/30/2007 3:58:00 AM

0

On Fri, Mar 30, 2007 at 12:29:12PM +0900, Ezra Zygmuntowicz wrote:
> On Mar 29, 2007, at 4:40 PM, Chad Perrin wrote:
>
> >Could you provide an example? The following, an apparently direct
> >translation from PHP (as far as I can tell), doesn't work.
> >
> >index.rhtml:
> > <%
> > title = "Home"
> > require 'template.rhtml'
> > %>
> >
> >template.rhtml:
> > <html>
> > <head>
> > <title>Welcome <%= title %></title>
> > </head>
> > <body>
> > <p>blah blah blah</p>
> > </body>
> > </html>
>
> Erb doesn't do anything like that by default. But you can fudge it
> pretty easily:
>
> ez work $ cat include.rb
> require 'erb'
> def _include(template, bind=binding)
> erb = ERB.new(IO.read(File.join(File.dirname(__FILE__),template)))
> erb.result(bind)
> end
> ez work $ cat foo.rhtml
> <% require 'include' %>
> <% 10.times do %>
> <%= _include 'bar.rhtml' %>
> <% end %>
> ez socialpicks $ cat bar.rhtml
> <%= "hello world!" %>

Thank you. I was hoping there was something simpler (using eruby.cgi
rather than erb) than rolling my own, but I do appreciate your example
and will put it (or something like it) to work. I guess I just assumed
too much about how eruby would work. At least now I won't waste my time
trying to figure out what I'm doing wrong.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
"It's just incredible that a trillion-synapse computer could actually
spend Saturday afternoon watching a football game." - Marvin Minsky

John Joyce

3/30/2007 6:06:00 AM

0

If it's the eruby.cgi at Dreamhost
that doesn't work very well...

On Mar 30, 2007, at 5:09 AM, Chad Perrin wrote:

> NOTE: I'll try one more time. For some reason, this message hasn't
> been getting through. It occurred to me that the mailing list
> might be
> discarding messages with (X)HTML in them, so I replaced all greater-
> than
> and less-than symbols with the corresponding bracket symbols. Please
> pretend, while reading this, that it's all valid XHTML -- and please
> forgive me if all three messages ultimately make it through.
>
>
> I've been playing with eruby a little bit -- a VERY LITTLE bit, mind
> you. I haven't gotten far yet.
>
> It seems like an excellent way to finally get away from PHP in certain
> types of work I do. The problem is that some of the concepts of PHP
> don't seem to translate directly, and the only howtos I'm able to find
> for eruby are anemic at best. There's plenty of stuff about how to
> install it, and precious little about how to make use of it.
>
> My current frustration is figuring out how to achieve the following:
>
> 1. an index.rhtml page that defines some variables
>
> 2. a template.whatever page that contains an XHTML page with
> evaluation
> of ruby variables defined in the index.rhtml page to produce
> page-specific content
>
> In PHP, it would be something like the following example.
>
> index.php:
> [?php
> $title = "Home";
> require_once('template.php');
> ?]
>
> template.php:
> [html]
> [head]
> [title]Welcome [?php echo $title ?][/title]
> [/head]
> [body]
> [p]blah blah blah[/p]
> [/body]
> [/html]
>
> So . . . ideas? How would I go about achieving the same results with
> eruby? Unless and until I can figure this out, there's a HUGE
> roadblock
> in my way of the application of the DRY principle.
>
> --
> CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
> "The first rule of magic is simple. Don't waste your time waving your
> hands and hopping when a rock or a club will do." - McCloctnick the
> Lucid
>