Ruby and PHP code in html-page

For example I have such html-page:

test.html

<% puts 'hello' %>
<?php phpinfo(); ?>

===========

Main web-server script-language is Ruby.
I want to include PHP-code.

Is it posiible?

On 7/12/07, Vadim S. [email protected] wrote:

===========

Main web-server script-language is Ruby.
I want to include PHP-code.

Is it posiible?

It is possible if you run the code first through ERB and then through
PHP or vice versa.

How slow it will be depends on your deployment type - i.e. whether do
you run PHP and/or Ruby as CGI, mod_ruby, using mongrel etc.

If mod_php allows you to define a post_request filter, that might
help. Or you could create a subrequest to php, and than process its
output.

I see a maintainability problem here - two languages intermixed.
The easier way would be to separate all the php or ruby stuff to a
separate webservice/subpage, and include its output to the main page.

…and if you want to really inter-mix ruby and php, that would be a
nightmare :wink:
as in:

<% 1…2.each do |i| %>

<?php ?>

<% end %>

J.

On Jul 12, 2007, at 9:23 AM, Jano S. wrote:

%>

How slow it will be depends on your deployment type - i.e. whether do
…and if you want to really inter-mix ruby and php, that would be a

I’ve been thinking about this as well lately.
Since Rails is overkill for a lot of little things, it would be nice
if ERb had more ability.
ERb lacks some of the functionality and flexibility of embedded php.
With php you can just include a file, but ERb doesn’t let you do that
(AFAIK).

John J.

On 2007-07-12, John J. [email protected] wrote:

With php you can just include a file, but ERb doesn’t let you do
that (AFAIK).

<%= IO.read “yes_it_does.txt” %>

Jeremy H.

On Jul 12, 12:43 pm, Jeremy H. [email protected] wrote:

On 2007-07-12, John J. [email protected] wrote:

With php you can just include a file, but ERb doesn’t let you do
that (AFAIK).

<%= IO.read “yes_it_does.txt” %>

Not quite when you want the included file to have ERB commands in it:

C:>type main.erb
<%=$foo%>
<%=IO.read(‘inc.erb’)%>

C:>type inc.erb
<%=$foo%>

C:>irb
irb(main):001:0> require ‘erb’
irb(main):002:0> $foo = ‘Hello World’
irb(main):003:0> ERB.new( IO.read( ‘main.erb’ ) ).run
Hello World
<%=$foo%>

John J. wrote:

I’ve been thinking about this as well lately.
Since Rails is overkill for a lot of little things, it would be nice if
ERb had more ability.

Look at Ramaze or Nitro. Or IOWA or Cerise. Or Merb. Or Camping.

Or roll your own.

Rule your world.


James B.

http://www.rubyaz.org - Hacking in the Desert
http://www.jamesbritt.com - Playing with Better Toys

On 2007-07-12, Phrogz [email protected] wrote:

On Jul 12, 12:43 pm, Jeremy H. [email protected] wrote:

On 2007-07-12, John J. [email protected] wrote:

With php you can just include a file, but ERb doesn’t let you do
that (AFAIK).

<%= IO.read “yes_it_does.txt” %>

Not quite when you want the included file to have ERB commands in it:

Good point!

<%= ERB.new(IO.read(“how_about_this_then.erb”)).result(binding) %>

Jeremy H.

John J. wrote:

I’ve been thinking about this as well lately.
Since Rails is overkill for a lot of little things, it would be nice if
ERb had more ability.
ERb lacks some of the functionality and flexibility of embedded php.
With php you can just include a file, but ERb doesn’t let you do that
(AFAIK).

Couldn’t you just put the Erb inside the html? Like:

<%= Erb.new(IO.read(filename)).result %>


Travis W.

“Programming in Java is like dealing with your mom –
it’s kind, forgiving, and gently chastising.
Programming in C++ is like dealing with a disgruntled
girlfriend – it’s cold, unforgiving, and doesn’t tell
you what you’ve done wrong.”

On Jul 12, 2007, at 5:46 PM, Travis D Warlick Jr wrote:

Couldn’t you just put the Erb inside the html? Like:
“Programming in Java is like dealing with your mom –
it’s kind, forgiving, and gently chastising.
Programming in C++ is like dealing with a disgruntled
girlfriend – it’s cold, unforgiving, and doesn’t tell
you what you’ve done wrong.”

Let me toy with it a bit and see how much I love/hate it.

JJ

On 7/12/07, John J. [email protected] wrote:

that (AFAIK).

That’s OK I guess, but it’s not as graceful as usual Ruby things.
I guess I’m going to look into Camping and those others.
I checked out the Merb site once but it was all RDoc with no solid
examples.

You can of course make it more graceful:

def erb_include(path, b = nil)
b = binding unless b
ERB.new(IO.read(path)).result(b)
end

<%= erb_include(“file.erb”) %>

It’s still a bit icky w/ local vars :frowning: [But that’s part of the reason I
don’t like templating like this anyway]

CGI is just too ugly, it might as well be Perl for that, I wish

On Jul 12, 2007, at 6:00 PM, Logan C. wrote:

With php you can just include a file, but ERb doesn’t let you do

def erb_include(path, b = nil)

It’s too bad that ERb itself isn’t better tutorialized.

Doesn’t seem to work with DreamHost’s eruby.cgi or with their
erb.cgi …
or I’m just an idiot groping in the dark. (mostly likely reason)

On Jul 12, 2007, at 4:50 PM, Jeremy H. wrote:

Not quite when you want the included file to have ERB commands in it:

Good point!

<%= ERB.new(IO.read(“how_about_this_then.erb”)).result(binding) %>

Jeremy H.

That’s OK I guess, but it’s not as graceful as usual Ruby things.
I guess I’m going to look into Camping and those others.
I checked out the Merb site once but it was all RDoc with no solid
examples.
CGI is just too ugly, it might as well be Perl for that, I wish
DreamHost would run mod_ruby…
I’m just finding that Rails is just way more than what I want or
need. Lots of cool stuff in it, but the problem is that there is just
so much there. Getting started is easy enough, but things start to
grind down to a crawl as you beginning unravelling all the details.
It’s like any big framework, it’s highly productive and useful AFTER
you’ve spent months learning it. There’s definitely a learning curve
and things start to get pretty complicated pretty quickly.

It’s too bad that ERb itself isn’t better tutorialized.

On Jul 12, 2007, at 6:25 PM, Jeremy H. wrote:

I guess I’m going to look into Camping and those others.

I’ve not yet decided if my head is twisted enough to survive being
wrapped around one of _why’s frameworks. :slight_smile:

Jeremy H.

Well I looked at Camping again. No. _why’s stuff is sometimes bent on
mental tangents of playfulness.
The file is one big mess. Also it requires ActiveRecord, which is of
course a marvel of marvels, but I’m trying to do the simple PHP type
of includes.
I got this to work fine:
<%= File.open(‘links.rhtml’).readlines %>
Along with this:
<% Dir.chdir(‘images’) %>
<% Dir.glob("*").each do |img| %>

<img src=<%= “images/#{img}” %> />
<%= img.to_s %>

<% end %>

The images seem to load slowly, which is odd, since they’re actually
being served from the file system. The ERb stuff just renders the
html based on what’s there in the directory.

So far so good. Your hints actually helped me think about ERb in
terms of standard Ruby rather than the prolific Rails views which
include lots of Rails-specific methods.

This is most promising, and I think I will begin posting some
tutorials on it so others can apply it.

Thanks ya’ll, I don’t know why ERb never clicked before, but now it
makes sense suddenly!
I can finally knock out simple stuff for a friend of mine to upload
images to his simple site without him being hassled by the details of
web design.
Now I just need to add some RMagick to thumbnail things and some YAML
to store metadata on the images and thumbnails and just a little
directory structure to make things organized.
This is gonna be much better for me than Rails.

John J.

On Jul 12, 2007, at 09:12 , Vadim S. wrote:

===========

Main web-server script-language is Ruby.
I want to include PHP-code.

Is it posiible?

Try using SSI for the php code.

On 2007-07-12, John J. [email protected] wrote:

On Jul 12, 2007, at 4:50 PM, Jeremy H. wrote:

<%= ERB.new(IO.read(“how_about_this_then.erb”)).result(binding) %>

That’s OK I guess, but it’s not as graceful as usual Ruby things.

I agree, though you can pretty it up somewhat (as is pointed out
elsewhere in this thread).

I guess I’m going to look into Camping and those others.

I’ve not yet decided if my head is twisted enough to survive being
wrapped around one of _why’s frameworks. :slight_smile:

Jeremy H.

On Jul 30, 2007, at 7:48 AM, Vadim S. wrote:

Try using SSI for the php code.

It might be possible to use ERb (Ruby) to insert the PHP.
And then have the server first process files with Ruby, then with PHP.
Depends on the server. It’s ugly, maybe a little slower, but probably
possible.

Wayne E. Seguin wrote:

On Jul 12, 2007, at 09:12 , Vadim S. wrote:

===========

Main web-server script-language is Ruby.
I want to include PHP-code.

Is it posiible?

Try using SSI for the php code.

SSI works super, but I have such problem:
I want to include in html-file some php-code one of counter-systems
written on PHP.
By using SSI, I must write php-code to some php-file and when this php
will be included, php-script don’t know about html-file, about first
request uri.
For counter-system it’s bad variant.

On Jul 30, 12:22 pm, John J. [email protected]
wrote:

I want to include PHP-code.
request uri.
For counter-system it’s bad variant.

Posted viahttp://www.ruby-forum.com/.

It might be possible to use ERb (Ruby) to insert the PHP.
And then have the server first process files with Ruby, then with PHP.
Depends on the server. It’s ugly, maybe a little slower, but probably
possible.

You may want to look at Erubis.
http://www.kuwata-lab.com/erubis/

jzakiya wrote:

On Jul 30, 12:22 pm, John J. [email protected]
wrote:

I want to include PHP-code.
request uri.
For counter-system it’s bad variant.

Posted viahttp://www.ruby-forum.com/.

It might be possible to use ERb (Ruby) to insert the PHP.
And then have the server first process files with Ruby, then with PHP.
Depends on the server. It’s ugly, maybe a little slower, but probably
possible.

You may want to look at Erubis.
http://www.kuwata-lab.com/erubis/

With Erubis I can “convert” Ruby-code to PHP. I need to insert PHP-code
in Ruby-project.

On Jul 31, 2:21 am, Vadim S. [email protected] wrote:

And then have the server first process files with Ruby, then with PHP.
Depends on the server. It’s ugly, maybe a little slower, but probably
possible.

You may want to look at Erubis.
http://www.kuwata-lab.com/erubis/

With Erubis I can “convert” Ruby-code to PHP. I need to insert PHP-code
in Ruby-project.

Posted viahttp://www.ruby-forum.com/.

Did you look at the Erubis documentation?
http://www.kuwata-lab.com/erubis/users-guide.04.html#lang