Created
December 5, 2012 21:12
-
-
Save danielauener/4219540 to your computer and use it in GitHub Desktop.
An easy view class to use in WordPress plugins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class My_View { | |
/** | |
* Path of the view to render | |
*/ | |
var $view = ""; | |
/** | |
* Variables for the view | |
*/ | |
var $vars = array(); | |
/** | |
* Construct a view from a file in the | |
*/ | |
public function __construct($view) { | |
if (file_exists(MY_PLUGIN_ABS_PATH."/views/".$view.".view.php")) { | |
$this->view = MY_PLUGIN_ABS_PATH."/views/".$view.".view.php"; | |
} else { | |
wp_die(__("View ".SPT_PLUGIN_ABS_PATH."/views/".$view.".view.php"." not found")); | |
} | |
} | |
/** | |
* set a variable which gets rendered in the view | |
*/ | |
public function set($name,$value) { | |
$this->vars[$name] = $value; | |
} | |
/** | |
* render the view | |
*/ | |
public function render() { | |
extract($this->vars,EXTR_SKIP); | |
ob_start(); | |
include $this->view; | |
echo ob_get_clean(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment