Need help converting PHP to Ruby / eRuby

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

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.

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/\.

James Nykiel wrote:

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

%>

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 %>
   <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:

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' %>
   Home &nbsp;&nbsp;::&nbsp;&nbsp;
<% else %>
   <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/\.

<%
  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}</a>"
     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; ") %>

···

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 %>