Need help converting PHP to Ruby / eRuby

J

James Nykiel

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

%>
 
J

Jano Svitok

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
 
J

James Nykiel

Jano said:
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

%>
 
J

Justin Collins

James said:
Jano said:
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
 
J

James Nykiel

Justin said:
James said:
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 %>
 
J

Jano Svitok

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top