Skip to content

Instantly share code, notes, and snippets.

@danielauener
Created December 5, 2012 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielauener/4219540 to your computer and use it in GitHub Desktop.
Save danielauener/4219540 to your computer and use it in GitHub Desktop.
An easy view class to use in WordPress plugins
<?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