# Need help converting PHP to Ruby / eRuby

**URL:** <https://rubytalk.org/t/need-help-converting-php-to-ruby-eruby/46215>\
**Category:** ruby-talk\
**Created:** [30 April 2008 20:08 UTC](https://rubytalk.org/t/need-help-converting-php-to-ruby-eruby/46215 "2008-04-30T20:08:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Nykiel](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@James\_Nykiel](https://rubytalk.org/u/James_Nykiel)\
**Post date:** [30 April 2008 20:08 UTC](https://rubytalk.org/t/need-help-converting-php-to-ruby-eruby/46215/1 "2008-04-30T20:08:04Z")

</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 '\<a href="index.php" title="Home"\>Home\</a\>  
&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 '\<a href="index.rhtml" title="Home"\>Home\</a\>  
&nbsp;&nbsp;::&nbsp;&nbsp;'  
end

%\>

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [30 April 2008 21:16 UTC](https://rubytalk.org/t/need-help-converting-php-to-ruby-eruby/46215/2 "2008-04-30T21:16:23Z")

</div>

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

> **···**
>
> On Wed, Apr 30, 2008 at 10:08 PM, James Nykiel 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.

---

<div class="post-metadata">

**Author:** ![James\_Nykiel1](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@James\_Nykiel1](https://rubytalk.org/u/James_Nykiel1)\
**Post date:** [30 April 2008 21:58 UTC](https://rubytalk.org/t/need-help-converting-php-to-ruby-eruby/46215/3 "2008-04-30T21:58:14Z")

</div>

Jano Svitok 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 '\<a href="index.rhtml" title="Home"\>Home\</a\>  
&nbsp;&nbsp;::&nbsp;&nbsp;'  
end

%\>

> **···**
>
> > On Wed, Apr 30, 2008 at 10:08 PM, James Nykiel wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Justin\_Collins](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Justin\_Collins](https://rubytalk.org/u/Justin_Collins)\
**Post date:** [1 May 2008 04:03 UTC](https://rubytalk.org/t/need-help-converting-php-to-ruby-eruby/46215/4 "2008-05-01T04:03:14Z")

</div>

James Nykiel wrote:

> Jano Svitok wrote:  
> &nbsp;&nbsp;
> 
> > > 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.  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > 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  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > 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 '\<a href="index.rhtml" title="Home"\>Home\</a\> &nbsp;&nbsp;::&nbsp;&nbsp;'  
> end
> 
> %\>

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

\<%

fname = File.basename ENV['SCRIPT\_FILENAME']

if fname == 'index.rhtml' %\>  
&nbsp;&nbsp;&nbsp;Home &nbsp;&nbsp;::&nbsp;&nbsp;  
\<% else %\>  
&nbsp;&nbsp;&nbsp;\<a href="index.rhtml" title="Home"\>Home\</a\>&nbsp;&nbsp;::&nbsp;&nbsp;  
\<% end %\>

-Justin

> **···**
>
> > > On Wed, Apr 30, 2008 at 10:08 PM, James Nykiel wrote:

---

<div class="post-metadata">

**Author:** ![James\_Nykiel1](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@James\_Nykiel1](https://rubytalk.org/u/James_Nykiel1)\
**Post date:** [2 May 2008 02:11 UTC](https://rubytalk.org/t/need-help-converting-php-to-ruby-eruby/46215/5 "2008-05-02T02:11:07Z")

</div>

Justin Collins wrote:

> James Nykiel 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' %\>  
> &nbsp;&nbsp;&nbsp;Home &nbsp;&nbsp;::&nbsp;&nbsp;  
> \<% else %\>  
> &nbsp;&nbsp;&nbsp;\<a href="index.rhtml" title="Home"\>Home\</a\>&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 %\>  
\<a href="index.rhtml" title="Home"\>Home\</a\> &nbsp;&nbsp;::&nbsp;&nbsp;  
\<% end %\>

\<% fname = File.basename ENV['SCRIPT\_FILENAME']  
if fname == 'contact.rhtml' %\>  
Contact &nbsp;&nbsp;::&nbsp;&nbsp;  
\<% else %\>  
\<a href="contact.rhtml" title="Contact"\>Contact\</a\>  
&nbsp;&nbsp;::&nbsp;&nbsp;  
\<% end %\>

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [2 May 2008 07:07 UTC](https://rubytalk.org/t/need-help-converting-php-to-ruby-eruby/46215/6 "2008-05-02T07:07:44Z")

</div>

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

&nbsp;&nbsp;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)  
&nbsp;&nbsp;&nbsp;fname = File.basename ENV['SCRIPT\_FILENAME']  
&nbsp;&nbsp;&nbsp;menu.map {|page\_file, page\_title| menu\_item(fname, page\_file,  
page\_title)}.join(separator)  
end

MENU = [  
&nbsp;&nbsp;['index.rhtml, 'Home'],  
&nbsp;&nbsp;['contact.rhtml', 'Contact']  
]  
%\>

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

> **···**
>
> On Fri, May 2, 2008 at 4:11 AM, James Nykiel \<lmv314@gmail.com\> wrote:
> 
> > 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 %\>  
> > \<a href="index.rhtml" title="Home"\>Home\</a\> &nbsp;&nbsp;::&nbsp;&nbsp;  
> > \<% end %\>
> > 
> > \<% fname = File.basename ENV['SCRIPT\_FILENAME']  
> > if fname == 'contact.rhtml' %\>  
> > Contact &nbsp;&nbsp;::&nbsp;&nbsp;  
> > \<% else %\>  
> > \<a href="contact.rhtml" title="Contact"\>Contact\</a\>
> > 
> > &nbsp;&nbsp;::&nbsp;&nbsp;  
> > \<% end %\>
