get_include_contents (function from PHP.net examples) help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

get_include_contents (function from PHP.net examples) help

Post by jayshields »

Right.

I've coded a templating class. It's usage is like this:

Code: Select all

//...define other vars...
$template->content_include_file = 'something.php';
//...define other vars...

echo $template->make_content_block();
make_content_block is like this:

Code: Select all

function make_content_block() {
			$content_blocks1 =
			'<div class="content">' . "\n";
				'<p>' . "\n";
				$this->get_include_contents('blocks/' . $this->content_include_file);
				$content_blocks1 .=
				'</p>' . "\n";
			$content_blocks1 .=
			'</div>' . "\n";
		}

		return $content_blocks1;
	}
get_include_contents:

Code: Select all

function get_include_contents($filename) {
		if(is_file($filename)) {
			ob_start();
			include($filename);
			$contents = ob_get_contents();
			ob_end_clean();
			return $contents;
		} else {
			echo $filename . ' is not a valid file!';
			return FALSE;
		}
	}
It doesn't work and I can't understand why. I'm not getting an unable to open stream error, I know somethings in the file (just some simple echo statements for testing), it never echo's $filename is not a valid file.

It just shows nothing.

Is it something to do with defining the function in the same class that I'm calling it from?

I've also tried replacing the include() statement in get_include_contents() with exec(file_get_contents()), but that has the same effect.

If I insert an echo msg before return contents it shows it.

If I get rid of my get_include_contents() call in my make_content_block() function and just use include it does exactly what I want but it gets output to the browser at the wrong time in my script, so it's out of place.

All I want to do is run the file given to the function and store everything thats outputted to the browser in a string instead of actually outputting it to the browser.

Anyone got any ideas for me?

Thanks.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

You can delete this thread. God I'm so stupid.

I needed to change:

Code: Select all

$content_blocks1 .=
                '<p>' . "\n";
                $this->get_include_contents('blocks/' . $this->content_include_file);
                $content_blocks1 .=
                '</p>' . "\n";
to

Code: Select all

$content_blocks1 .=
                '<p>' . "\n";
                $content_blocks1 .= $this->get_include_contents('blocks/' . $this->content_include_file);
                $content_blocks1 .=
                '</p>' . "\n";
Silly.

Sorry.
Post Reply