Is it possible to add print () output to a variable?
I have the following situation:
I have a php file that looks something like this:
title.php
<?php $content = '<h1>Page heading</h1>'; print($content);
I have a php file that looks like this:
page.php
<?php $content = '<div id="top"></div>'; $content.= $this->renderHtml('title.php'); print($content);
I have a renderHtml() function:
public function renderHtml($name) { $path = SITE_PATH . '/application/views/' . $name; if (file_exists($path) == false) { throw new Exception('View not found in '. $path); return false; } require($path); }
When I unload the content variable in page.php, it does not contain the contents of title.php. The contents of title.php are simply printed when it is called instead of being added to the variable.
I hope this is clear what I'm trying to do. If not, excuse me, please tell me what you need to know. :)
Thank you for your help!
PS
I found that there was already a question similar to mine. But it concerned Zend FW.
How to capture the output of a Zend view instead of actually outputting it
However, I think this is exactly what I want to do.
How do I configure a function to behave like this?
EDIT
Just wanted to share the final solution:
public function renderHtml($name) { $path = SITE_PATH . '/application/views/' . $name; if (file_exists($path) == false) { throw new Exception('View not found in '. $path); return false; } ob_start(); require($path); $output = ob_get_clean(); return $output; }
php
Peeehaa
source share