output buffering

M

mark

Hi,
Is there any way to do php-style output buffering with ruby or rails?
as in:

ob_start()
%>
html goes in here
<%
html=ob_get_contents()
ob_end_clean()
puts html
 
M

mark

Thanks for the reply Alex but this doesn't seem to work, maybe it's a
mod_ruby thing but nothing after
$stdout = tmp_out
gets sent no matter what I do with $stdout
 
A

Alex Fenton

mark said:
Thanks for the reply Alex but this doesn't seem to work, maybe it's a
mod_ruby thing but nothing after
$stdout = tmp_out
gets sent no matter what I do with $stdout

I think mod_ruby overloads some of ruby's standard IO mechanisms, eg the Kernel#puts method. You probably need to restore 'standard' STDOUT in a slightly different way in mod_ruby. There's a mod_ruby mailing list where you might get a quicker answer, or try googling STDOUT + mod_ruby etc

cheers
alex
 
E

eduard

------=_Part_837_9840112.1137690613804
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Mark,

I am having a similar problem. Have you ever figured out how to do
ob_start() in Ruby?

thanks,
eduard

hi


In ruby, generally, you'd achieve the same thing by redirecting $stdout.
Something like:

require 'stringio'
tmp_out =3D StringIO.new()

# redirect STDOUT
$stdout =3D tmp_out

puts "this and that" # would normally go to STDOUT

# restore default stdout
$stdout =3D STDOUT

tmp_out.rewind()
puts tmp_out.read() # print out what was sent to STDOUT

I don't know whether Ruby on Rails has any special facility for
temporarily
redirecting output of literal HTML snippets within a mixed Ruby-HTML
template (as the PHP function does, iirc). The rails mailing list should = be
able to help you with that one.

cheers
alex

------=_Part_837_9840112.1137690613804--
 
E

Eero Saynatkari

eduard said:
Mark,

I am having a similar problem. Have you ever figured out how to do
ob_start() in Ruby?

I am not familiar with PHP, would you be able to give an
example where this would be useful or what this is commonly
used for? This might enable finding a ruby equivalent.
thanks,
eduard


E
 
E

eduard

------=_Part_6192_5946798.1137785627973
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I was just trying to find an easy way set headers for each page.

Tom Fakes suggested this and it works great.

after_filter { |controller|

controller.response.headers['Content-Length'] =3D
controller.response.body.length

}

I am not familiar with PHP, would you be able to give an
example where this would be useful or what this is commonly
used for? This might enable finding a ruby equivalent.



E

------=_Part_6192_5946798.1137785627973--
 
P

patsplat

Hi Mark,
Hi,
Is there any way to do php-style output buffering with ruby or rails?

ruby-web, a replacement for the ruby web libraries, offers this
functionality:

http://www.ruby-web.org/manual/reference-output.html

I haven't finished testing a rails handler for ruby-web. These methods
are implemented on a Web::Connection class with tests:


http://svn.ruby-web.org/svn/patsplat/ruby-web/trunk/lib/web/connection.rb

http://svn.ruby-web.org/svn/patsplat/ruby-web/trunk/test/web/test.buffer.rb

You might be able to extract this functionality for use with rails /
etc.

Cheers,

Patrick
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top