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   ::  '; } else { echo 'Home   ::  '; } ?>

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

else
puts 'Home
  ::  ’
end

%>

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

else
puts 'Home
  ::  ’
end

%>

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   ::  
<% else %>
Home  ::  
<% end %>

-Justin

On Fri, May 2, 2008 at 4:11 AM, James N. [email protected] wrote:

Home   ::  
<% end %>

<% fname = File.basename ENV[‘SCRIPT_FILENAME’]
if fname == ‘contact.rhtml’ %>
Contact   ::  
<% else %>
Contact

  ::  
<% 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”) %>
  ::  
<%= menu_item(fname, “contact.rhtml”, “Contact” %>
  ::  

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, "   ::   ") %>

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   ::  
<% else %>
Home  ::  
<% 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   ::  
<% else %>
Home   ::  
<% end %>

<% fname = File.basename ENV[‘SCRIPT_FILENAME’]
if fname == ‘contact.rhtml’ %>
Contact   ::  
<% else %>
Contact
  ::  
<% end %>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs