include - Accessing variables in included file through helper method in PHP -


i trying build php class let me include other php "template" files display. want of variables in scope called available in included file. here's catch, want pass off actual including helper method keep code dry. here have far:

/* templateloader.php */ class templateloader {   public function foo() {     $var = "foo!";     //works     include 'template.php';   }    public function bar() {     $var = "bar!";     //doesn't work     $this->render("template");   }    private function render( $name ) {     include $name . '.php';   } }  /* template.php */ <?php echo $var; ?> 

my question is: how can accomplish behaviour of including template directly in original method while still using helper method "heavy lifting"? appreciate can give!

this first came mind - i'm not sure much, i'm not sure of generic alternative. captures of current variables get_defined_vars(), , passes them render(), they extract()ed, , accessible included file.

you filter return get_defined_vars() before pass render(), should work.

public function bar() {     $var = "bar!";     $this->render("template", get_defined_vars()); }  private function render( $name, &$vars) {     extract( $vars);     include $name . '.php'; } 

you filter return get_defined_vars() before pass render(), should work.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -