Ezra Zygmuntowicz
3/30/2007 3:29:00 AM
On Mar 29, 2007, at 4:40 PM, Chad Perrin wrote:
> Could you provide an example? The following, an apparently direct
> translation from PHP (as far as I can tell), doesn't work.
>
> index.rhtml:
> <%
> title = "Home"
> require 'template.rhtml'
> %>
>
> template.rhtml:
> <html>
> <head>
> <title>Welcome <%= title %></title>
> </head>
> <body>
> <p>blah blah blah</p>
> </body>
> </html>
Chad-
Erb doesn't do anything like that by default. But you can fudge it
pretty easily:
ez work $ cat include.rb
require 'erb'
def _include(template, bind=binding)
erb = ERB.new(IO.read(File.join(File.dirname(__FILE__),template)))
erb.result(bind)
end
ez work $ cat foo.rhtml
<% require 'include' %>
<% 10.times do %>
<%= _include 'bar.rhtml' %>
<% end %>
ez socialpicks $ cat bar.rhtml
<%= "hello world!" %>
ez work $ erb foo.rhtml
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
You can't use 'include' because of Module#include so I used
_include. Have fun!
Cheers
-- Ezra Zygmuntowicz
-- Lead Rails Evangelist
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)