Added Canvas 1.1.0, originally not under SCM so no historical development records...
[canvas.git] / library / View.php
blob26a4b956c95ed10f96a6816639f8e10259e51db9
1 <?php
2 // @role View
3 // @title View Class
4 // @author Matt Todd
5 // @date 2005-12-28
6 // @desc Handles rendering the results to the user, possibly with Smarty
8 include_once('stdexception.php');
10 // classes
11 class View { // should implement the IView interface
12 // render
13 public static function render($response, $params = null) {
14 $view_name = $response->controller;
15 $helper_name = file_exists("{$response->controller}_helper") ? "{$response->controller}_helper" : "application_helper";
17 $smarty = new Smarty();
19 $smarty->left_delimiter = "<" . "%"; // split them up as to keep them from parsing accidentally
20 $smarty->right_delimiter = "%". ">"; // ditto
21 $smarty->template_dir = "./views/{$view_name}/";
22 $smarty->compile_dir = "./views/{$view_name}/compile/";
23 $smarty->cache_dir = "./views/{$view_name}/cache/";
24 $smarty->config_dir = "./views/{$view_name}/config/";
26 // register helper functions
27 self::register_helper_functions($helper_name, $smarty);
29 // register while block
30 $smarty->register_block('while', array(get_class(), 'while_block'));
32 $smarty->assign('response', $response);
33 foreach($response->respond() as $key=>$value) {
34 // if($key == "response") continue;
35 $smarty->assign($key, $value);
38 // render layout if set, action template if not...
39 if($response->layout == null) {
40 $smarty->display($response->template);
41 } else {
42 $smarty->display($response->layout);
46 // this static method will analyze the methods of the helper class that is to be associated
47 // with the views and will register these functions appropriately (automating the registration
48 // process)
49 private static function register_helper_functions($helper_name, &$smarty) {
50 $helper_class = new ReflectionClass($helper_name);
51 $filters = self::find_helper_filters($helper_class);
52 foreach($helper_class->getMethods() as $method) {
53 if(in_array($method->name, $filters)) {
54 $smarty->register_modifier($method->name, array($helper_name, $method->name)); continue; // register filters as such
56 $smarty->register_function($method->name, array($helper_name, $method->name));
59 private static function find_helper_filters($helper_class) {
60 $properties = $helper_class->getProperties();
61 $helper_class_name = $helper_class->getName();
62 $helper_instance = new $helper_class_name();
63 foreach($properties as $filter) {
64 if($filter->getName() == "treat_as_filter") return $filter->getValue($helper_instance);
68 // custom tags/functions for Smarty
69 public static function while_block($params, $content, &$smarty, &$repeat) {
70 $name = $params['name'];
71 $value = $params['value'];
72 $$name = $value;
73 return $content;
77 class ViewException extends StdException {}