Pass Variables to View

extract() them in the view for easier access
This commit is contained in:
Brian Buller 2012-06-11 22:20:26 -05:00
parent 61f4566fdb
commit a080618ba0
1 changed files with 18 additions and 6 deletions

View File

@ -3,6 +3,9 @@
class Controller { class Controller {
public function __construct() { } public function __construct() { }
public function load_models($model=NULL) {
$this->load_model($model);
}
public function load_model($model=NULL) { public function load_model($model=NULL) {
// All models end with '_model' // All models end with '_model'
if(is_array($model)) { if(is_array($model)) {
@ -15,6 +18,9 @@ class Controller {
$this->_load_files($model, "models"); $this->_load_files($model, "models");
} }
public function load_libraries($library=NULL) {
$this->load_library($library);
}
public function load_library($library=NULL) { public function load_library($library=NULL) {
// All libraries end with '_library' // All libraries end with '_library'
if(is_array($library)) { if(is_array($library)) {
@ -27,14 +33,17 @@ class Controller {
$this->_load_files($library, "libraries"); $this->_load_files($library, "libraries");
} }
public function load_view($views=NULL) { public function load_views($views=NULL, $vars=array()) {
$this->load_view($views,$vars);
}
public function load_view($views=NULL, $vars=array()) {
// No restrictions on view names // No restrictions on view names
$this->_load_files($views, "views", true); $this->_load_files($views, "views", true, $vars);
} }
// Runs through a potential array of files // Runs through a potential array of files
// Checks for existence, then _load_file // Checks for existence, then _load_file
public function _load_files($a=null, $func=null, $multi=false) { public function _load_files($a=null, $func=null, $multi=false, $vars=array()) {
if(isset($a) && isset($func)) { if(isset($a) && isset($func)) {
if(is_array($a)) { if(is_array($a)) {
foreach($a as $aa) { foreach($a as $aa) {
@ -43,7 +52,7 @@ class Controller {
} else { } else {
$f = $func."/".$aa.".php"; $f = $func."/".$aa.".php";
} }
$this->_load_file($f, ($multi===true)); $this->_load_file($f, ($multi===true), $vars);
} }
} else { } else {
if(defined('PHP_DIR')) { if(defined('PHP_DIR')) {
@ -51,13 +60,16 @@ class Controller {
} else { } else {
$f = $func."/".$a.".php"; $f = $func."/".$a.".php";
} }
$this->_load_file($f, ($multi===true)); $this->_load_file($f, ($multi===true), $vars);
} }
} }
} }
// Checks if the file exists and includes it // Checks if the file exists and includes it
public function _load_file($filename=NULL,$multi=false) { public function _load_file($filename=NULL,$multi=false,$vars=null) {
if(isset($vars)&&is_array($vars)) {
extract($vars);
}
if(isset($filename) && file_exists($filename)) { if(isset($filename) && file_exists($filename)) {
if($multi) { if($multi) {
include($filename); include($filename);