0

I want to implement a pretty basic rjs kind utility in php. For those who have no prior knowledge of it, the rjs utility in Ruby on Rails returns application\javascript content-type for ajax requests. This is pretty handy when we want to change the inner html of two different divs through ajax in one request.

So what I need is to read html stream of some *.tpl.php file so that from the server side code, I can return javascript strings as:

$rjsStr = "$('#div1').html(".Util::getHtmlFromTpl($outputParams1, 'tplfile1.tpl.php').");";
$rjsStr .= "$('#div2').html(".Util::getHtmlFromTpl($outputParams2, 'tplfile2.tpl.php').");";
return $rjsStr;

Now my ajax request would look like $.ajax({url:'/controller/action.php?param1=1&param2=2',success:function(result){eval(result);}})

Edit The problem is that I don't know how to get the html stream of php file. I simply tried include('path_to_file/tplfile1.tpl.php'); but that doesn't return the html stream as a variable.
PS: I don't want to use file_get_contents for the security reasons.

Community
  • 1
  • 1
Gaurav Pandey
  • 2,798
  • 4
  • 28
  • 41
  • So what is your problem? You said **you** want to implement this util. Then go ahead! BTW since, you're going to evaluate the answer as js code, I'd suggest using `$.getScript`. – kirilloid Dec 30 '13 at 11:05

1 Answers1

0

include('path_to_file/tplfile1.tpl.php'); includes code and executes it. If you need to just read the file, then you may use file_get_contents, but I suppose, you need rendered template as input.

Then you need to use output buffering.

ob_start();
include('path_to_file/tplfile1.tpl.php');
$rendered_tpl = ob_get_clean();
// now you may strip excessive part, escape html and do with it whatever you want
kirilloid
  • 14,011
  • 6
  • 38
  • 52