Page 1 of 1

variable scope for view

Posted: Fri Sep 08, 2006 2:54 pm
by Luke
I am building an application right now and I am throwing up a quick-and-dirty framework for it because I just don't have the time to do much more. I have a simple function for capturing a file's html into a variable to use for my view:

Code: Select all

function capture_file($file){
	ob_start();
	include $file;
	return ob_get_clean();
}
The problem is that variables declared in the $file are inside of this function and therefor have its scope. What can I do to acheive this same functionality, but not have to send capture_file all of the variable that $file needs... ?

Posted: Fri Sep 08, 2006 2:59 pm
by feyd
Are the variables it needs globals? If so, your own version of extract() could probably be useful.

Otherwise, it'll have to be some variant on that theme or you'll need to specify more specific context.

Posted: Fri Sep 08, 2006 3:06 pm
by Luke
no... the variables it needs are any dynamic content within that view... such as: "hello, $username"

Re: variable scope for view

Posted: Fri Sep 08, 2006 3:18 pm
by Christopher
I tend to do it like this to keep vars out of the local namespace:

Code: Select all

class Template_Php {
var $file = '';
var $tags = array();

function set($tag, $value) {
	$this->tags[$tag] = $value;
}

function render(){
	ob_start();
	extract($this->tags);
	include $this->file;
	return ob_get_clean();
}

}

Posted: Fri Sep 08, 2006 3:19 pm
by Luke
good call... this is the only part of the framework where I wasn't using OOP... I will just convert what I've already done to use a view object. Thanks buddy.

Posted: Fri Sep 08, 2006 3:33 pm
by John Cartwright

Code: Select all

class Template
{
   public function __construct() { }
 
   public function __set($name, $value)
   { 
      if (isset($this->$name)) {
         //spit error about variable existing already
      }
  
      $this->$name = $val;
   }   

   public function capture_file($file)
  {
        ob_start();
        include $file;
        return ob_get_clean();
   }
}

Code: Select all

<html>
   <body>
      <?php echo $this->somevar; ?>
   </body>
</html>

Code: Select all

$template = new Template();
$template->title = 'Home';
You can take advantage of the __set method in this case, or just use another method to register variables to the class to give your template file the variable scope. There are so many different ways, just play around for a bit.

Edit | Damn I was late :oops:

Posted: Fri Sep 08, 2006 3:40 pm
by Luke
can't use __set cuz it's php4. (which is really why I am just kind of throwing it together... I have a sneaking suspicion I will have to rebuild it anyway so I can merge it with a php5 project... man I love this stuff!! Image

Posted: Wed Sep 13, 2006 8:59 pm
by John Cartwright
create a setter method then

Code: Select all

public function setvar($name, $value)
   {
      if (isset($this->$name)) {
         //spit error about variable existing already
      }
 
      $this->$name = $value;
   }

Code: Select all

$template = new Template();
$template->setvar('title', 'Home');

Posted: Wed Sep 13, 2006 11:24 pm
by Luke
This is what I am using for now... I will spruce it up when I can get around to it.

Code: Select all

class View{
	var $_vars = array();
	function set($name, $value){
		$this->_vars[$name] = $value;
	}
	function render($file){
	//TODO: check that $file is really a file
		ob_start();
		extract($this->_vars);
		include $file;
		return ob_get_clean();
	}
}