cgm225 Posted April 19, 2008 Share Posted April 19, 2008 Currently I have a simple Model-View-Controller, in which I have a simple View class. The View class has a render method that receives an array from a helper class, and this array ($this->viewData) is turned into variables which are then used in a template. So the flow is like this: An array is passed to the View class -> the array keys and values are turned into variables -> those variables are used in a template. An example would be: [website_title] => Example.com -----> $website_title = Example.com; -----> echo $website_title (within the title tags in the template) However, I also want to have default variables provided in the class somehow such that, if a required variable is not provided in the passed array, then the default value would be loaded. So, with the example above, if the passed array does not contain the website_title, then the default website_title will be loaded from somewhere. Currently, my View class does the behavior I have just described (see below). However, I feel like there is probably a better way to do this instead of including "hard coded" variables in the class. Any ideas? class View extends ActionController { public function render() { //Default variables, (To be placed in an outside/included file(?)) $title = "Example.com"; //This foreach function goes over all of the elements in $this->viewData array, creates //a variable for every value, and the name of the value (the variable name) is the key's value. foreach ($this->viewData as $key => $value) { $$key = $value; } //Including a template file within which variables are echoed require_once $this->pageDir . "/default.tpl"; } } Thank you all in advance! Link to comment https://forums.phpfreaks.com/topic/101880-help-with-default-variables-in-my-simple-view-class-in-my-model-view-controller/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.