# Ruby and PHP code in html-page

**URL:** <https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221>\
**Category:** Ruby\
**Created:** [July 12, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221 "2007-07-12T15:12:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 12, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/1 "2007-07-12T15:12:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 12, 2007, 4:24pm UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/2 "2007-07-12T16:24:23Z")

</div>

On 7/12/07, Vadim S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 😉  
as in:

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

\<?php ?\>

\<% end %\>

J.

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 12, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/3 "2007-07-12T20:00:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 12, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/4 "2007-07-12T20:47:07Z")

</div>

On 2007-07-12, John J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 12, 2007, 8:57pm UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/5 "2007-07-12T20:57:08Z")

</div>

On Jul 12, 12:43 pm, Jeremy H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 2007-07-12, John J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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%\>

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 12, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/6 "2007-07-12T20:30:05Z")

</div>

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](http://www.rubyaz.org) - Hacking in the Desert  
[http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 13, 2007, 12:18am UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/7 "2007-07-13T00:18:33Z")

</div>

On 2007-07-12, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jul 12, 12:43 pm, Jeremy H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > On 2007-07-12, John J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 13, 2007, 12:47am UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/8 "2007-07-13T00:47:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 13, 2007, 12:57am UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/9 "2007-07-13T00:57:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 13, 2007, 1:01am UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/10 "2007-07-13T01:01:38Z")

</div>

On 7/12/07, John J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 ☹ [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

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 13, 2007, 1:10am UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/11 "2007-07-13T01:10:42Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 13, 2007, 12:50am UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/12 "2007-07-13T00:50:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 13, 2007, 1:54am UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/13 "2007-07-13T01:54:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 15, 2007, 1:49pm UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/14 "2007-07-15T13:49:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 13, 2007, 1:25am UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/15 "2007-07-13T01:25:43Z")

</div>

On 2007-07-12, John J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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. 🙂

Jeremy H.

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 30, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/16 "2007-07-30T18:23:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 30, 2007, 2:48pm UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/17 "2007-07-30T14:48:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 31, 2007, 5:15am UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/18 "2007-07-31T05:15:26Z")

</div>

On Jul 30, 12:22 pm, John J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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/](http://www.kuwata-lab.com/erubis/)

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 31, 2007, 8:21am UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/19 "2007-07-31T08:21:38Z")

</div>

jzakiya wrote:

> On Jul 30, 12:22 pm, John J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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/](http://www.kuwata-lab.com/erubis/)

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

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [July 31, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221/20 "2007-07-31T21:36:35Z")

</div>

On Jul 31, 2:21 am, Vadim S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](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](http://www.kuwata-lab.com/erubis/users-guide.04.html#lang)

[Next page](https://www.ruby-forum.com/t/ruby-and-php-code-in-html-page/109221.md?page=2)
