# Need help converting PHP to Ruby / eRuby

**URL:** <https://www.ruby-forum.com/t/need-help-converting-php-to-ruby-eruby/136333>\
**Category:** Ruby\
**Created:** [April 30, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/need-help-converting-php-to-ruby-eruby/136333 "2008-04-30T22:08:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![asmodi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@asmodi](https://www.ruby-forum.com/u/asmodi)\
**Post date:** [April 30, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/need-help-converting-php-to-ruby-eruby/136333/1 "2008-04-30T22:08:03Z")

</div>

Hi,

I am new to Ruby / eRuby templating and need some help converting from  
PHP, I have two web pages that do what I need using PHP but I can figure  
out how to do the same with Ruby / eRuby templating, could someone  
please have a look and guide me?

What seems to be happening is that the Ruby / eRuby variable? $file is  
being set to menu.rhtml while the PHP variable? $file is being set to  
index.php, which is what I want the Ruby / eRuby to do also.

## PHP

# index.php

# Menu item name is non-link if page name is the same as menu item name,

if not then menu item name is link.

\<?php include("./menu.php"); ?\>
# menu.php

# Menu item name is non-link if page name is the same as menu item name,

if not then menu item name is link.

\<?php $path = ($\_SERVER['SCRIPT\_FILENAME']); $file = basename($path); if ($file == 'index.php') { echo 'Home &nbsp;&nbsp;::&nbsp;&nbsp;'; } else { echo 'Home &nbsp;&nbsp;::&nbsp;&nbsp;'; } ?\>
## Ruby / eRuby

# index.rhtml

# Menu item name is non-link if page name is the same as menu item name,

if not then menu item name is link.

\<%

ERuby.import (’./header.rhtml’)

%\>

# menu.rhtml

# Menu item name is non-link if page name is the same as menu item name,

if not then menu item name is link.

\<%

require ‘pathname’  
path = Pathname.new( **FILE** ).realpath.to\_s  
file = File.basename path

if file == ‘index.rhtml’  
puts 'Home &nbsp;&nbsp;::&nbsp;&nbsp;’

else  
puts 'Home  
&nbsp;&nbsp;::&nbsp;&nbsp;’  
end

%\>

---

<div class="post-metadata">

**Author:** ![asmodi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@asmodi](https://www.ruby-forum.com/u/asmodi)\
**Post date:** [April 30, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/need-help-converting-php-to-ruby-eruby/136333/2 "2008-04-30T23:17:28Z")

</div>

On Wed, Apr 30, 2008 at 10:08 PM, James N. wrote:

> Hi,
> 
> I am new to Ruby / eRuby templating and need some help converting from  
> PHP, I have two web pages that do what I need using PHP but I can figure  
> out how to do the same with Ruby / eRuby templating, could someone  
> please have a look and guide me?
> 
> What seems to be happening is that the Ruby / eRuby variable? $file is  
> being set to menu.rhtml while the PHP variable? $file is being set to  
> index.php, which is what I want the Ruby / eRuby to do also.

Hi,

try using ENV[‘SCRIPT\_FILENAME’] or $0 instead of **FILE**.

**FILE** is always the name of _current_ file (i.e. the one being  
parsed).  
$0 is the name of the “started” file (the one that was started from  
outside).  
SCRIPT\_FILENAME is CGI variable.

Note: not tested, just hints.

Jano

---

<div class="post-metadata">

**Author:** ![asmodi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@asmodi](https://www.ruby-forum.com/u/asmodi)\
**Post date:** [April 30, 2008, 11:58pm UTC](https://www.ruby-forum.com/t/need-help-converting-php-to-ruby-eruby/136333/3 "2008-04-30T23:58:14Z")

</div>

Jano S. wrote:

> On Wed, Apr 30, 2008 at 10:08 PM, James N. wrote:
> 
> > Hi,
> > 
> > I am new to Ruby / eRuby templating and need some help converting from  
> > PHP, I have two web pages that do what I need using PHP but I can figure  
> > out how to do the same with Ruby / eRuby templating, could someone  
> > please have a look and guide me?
> > 
> > What seems to be happening is that the Ruby / eRuby variable? $file is  
> > being set to menu.rhtml while the PHP variable? $file is being set to  
> > index.php, which is what I want the Ruby / eRuby to do also.
> 
> Hi,
> 
> try using ENV[‘SCRIPT\_FILENAME’] or $0 instead of **FILE**.
> 
> **FILE** is always the name of _current_ file (i.e. the one being  
> parsed).  
> $0 is the name of the “started” file (the one that was started from  
> outside).  
> SCRIPT\_FILENAME is CGI variable.
> 
> Note: not tested, just hints.
> 
> Jano

Jano,

Thanks for the hint, got it working!

This is the change that I made to make things work the way I needed.

## Ruby / eRuby

# menu.rhtml

# Menu item name is non-link if page name is the same as menu item name,

if not then menu item name is link.

\<%

fname = File.basename ENV[‘SCRIPT\_FILENAME’]

if fname == ‘index.rhtml’  
puts 'Home &nbsp;&nbsp;::&nbsp;&nbsp;’

else  
puts 'Home  
&nbsp;&nbsp;::&nbsp;&nbsp;’  
end

%\>

---

<div class="post-metadata">

**Author:** ![asmodi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@asmodi](https://www.ruby-forum.com/u/asmodi)\
**Post date:** [May 1, 2008, 6:03am UTC](https://www.ruby-forum.com/t/need-help-converting-php-to-ruby-eruby/136333/4 "2008-05-01T06:03:46Z")

</div>

James N. wrote:

> > > 
> > 
> > $0 is the name of the “started” file (the one that was started from  
> > Thanks for the hint, got it working!
> 
> %\>

I think the more typical way to do this is more like

\<%

fname = File.basename ENV[‘SCRIPT\_FILENAME’]

if fname == ‘index.rhtml’ %\>  
Home &nbsp;&nbsp;::&nbsp;&nbsp;  
\<% else %\>  
Home&nbsp;&nbsp;::&nbsp;&nbsp;  
\<% end %\>

-Justin

---

<div class="post-metadata">

**Author:** ![asmodi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@asmodi](https://www.ruby-forum.com/u/asmodi)\
**Post date:** [May 2, 2008, 9:08am UTC](https://www.ruby-forum.com/t/need-help-converting-php-to-ruby-eruby/136333/5 "2008-05-02T09:08:55Z")

</div>

On Fri, May 2, 2008 at 4:11 AM, James N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Home &nbsp;&nbsp;::&nbsp;&nbsp;  
> \<% end %\>
> 
> \<% fname = File.basename ENV[‘SCRIPT\_FILENAME’]  
> if fname == ‘contact.rhtml’ %\>  
> Contact &nbsp;&nbsp;::&nbsp;&nbsp;  
> \<% else %\>  
> Contact
> 
> &nbsp;&nbsp;::&nbsp;&nbsp;  
> \<% end %\>

\<%  
def menu\_item(current\_filename, page\_filename, page\_title)  
if current\_filename == page\_filename  
page\_title  
else  
“\<a href="#{page\_filename}"  
title="#{page\_title}"\>#{page\_title}”  
end  
end

fname = File.basename ENV[‘SCRIPT\_FILENAME’]  
%\>  
\<%= menu\_item(fname, “index.rhtml”, “Home”) %\>  
&nbsp;&nbsp;::&nbsp;&nbsp;  
\<%= menu\_item(fname, “contact.rhtml”, “Contact” %\>  
&nbsp;&nbsp;::&nbsp;&nbsp;

You can continue with storing the menu in an array, i.e.

\<%  
def make\_menu(menu, separator)  
fname = File.basename ENV[‘SCRIPT\_FILENAME’]  
menu.map {|page\_file, page\_title| menu\_item(fname, page\_file,  
page\_title)}.join(separator)  
end

MENU = [  
['index.rhtml, ‘Home’],  
[‘contact.rhtml’, ‘Contact’]  
]  
%\>

\<%= make\_menu(MENU, " &nbsp;&nbsp;::&nbsp;&nbsp; ") %\>

---

<div class="post-metadata">

**Author:** ![asmodi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@asmodi](https://www.ruby-forum.com/u/asmodi)\
**Post date:** [May 2, 2008, 4:11am UTC](https://www.ruby-forum.com/t/need-help-converting-php-to-ruby-eruby/136333/6 "2008-05-02T04:11:07Z")

</div>

Justin C. wrote:

> James N. wrote:
> 
> > > > 
> > > 
> > > $0 is the name of the “started” file (the one that was started from  
> > > Thanks for the hint, got it working!
> > 
> > %\>
> 
> I think the more typical way to do this is more like
> 
> \<%
> 
> fname = File.basename ENV[‘SCRIPT\_FILENAME’]
> 
> if fname == ‘index.rhtml’ %\>  
> Home &nbsp;&nbsp;::&nbsp;&nbsp;  
> \<% else %\>  
> Home&nbsp;&nbsp;::&nbsp;&nbsp;  
> \<% end %\>
> 
> -Justin

Justin,

Thanks for the help with this, do you know if or how I can combine the  
following:

## menu.rhtml

\<% fname = File.basename ENV[‘SCRIPT\_FILENAME’]  
if fname == ‘index.rhtml’ %\>  
Home &nbsp;&nbsp;::&nbsp;&nbsp;  
\<% else %\>  
Home &nbsp;&nbsp;::&nbsp;&nbsp;  
\<% end %\>

\<% fname = File.basename ENV[‘SCRIPT\_FILENAME’]  
if fname == ‘contact.rhtml’ %\>  
Contact &nbsp;&nbsp;::&nbsp;&nbsp;  
\<% else %\>  
Contact  
&nbsp;&nbsp;::&nbsp;&nbsp;  
\<% end %\>
