Jump to content

[SOLVED] extract and ob functions


scottybwoy

Recommended Posts

Hi,

 

I have a small script that's used for templating.  It's meant to extract $vars out of an object, read a file (.php with html syntax, except php placeholders like so <?=$var?>).  Then pass the contents to an echo.  It takes the vars, but they don't get entered into the placeholders.  Are there any settings in the php.ini I need to change or am I just doing it wrong.

 

Here's the code :

<?php
  function fetch($file) {
    print_r($this->vars);
    ob_start();          			// Start output buffering 
    extract($this->vars);     		// Extract the vars to local namespace
    include($this->path . $file); 	// Include the file 
    $contents = ob_get_contents(); 	// Get the contents of the buffer 
    ob_end_clean();        			// End buffering and discard 
    return $contents;       		// Return the contents 
  }
?>

The print_r echos : Array ( [title] => I Say Union, You Say Onion )

 

Let's say the file being passed has this contents :

<html>
<head>
<title><?=$title?></title>
</head>
<body>
This page has a title of <?=$title?>.
</body>
</html>

When $contents is echoes in the calling file, it just prints "Array ( [title] => I Say Union, You Say Onion )This page has a title of ."

 

Without putting the title in.  This is the first time I've used these functions so would be helpful for your clarification.

Link to comment
https://forums.phpfreaks.com/topic/135244-solved-extract-and-ob-functions/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.