PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/View.php

http://shozu.googlecode.com/
PHP | 310 lines | 168 code | 24 blank | 118 comment | 14 complexity | c3df531be90f087b2db687a882886de5 MD5 | raw file
  1. <?php
  2. namespace shozu;
  3. /**
  4. * View
  5. *
  6. * @package MVC
  7. */
  8. class View
  9. {
  10. /**
  11. * String of template file
  12. */
  13. private $file;
  14. /**
  15. * Array of template variables
  16. */
  17. private $vars = array();
  18. private $cache_id;
  19. private static $cache;
  20. /**
  21. * Assign the template path
  22. *
  23. * @param string $file Template path (absolute path or path relative to the templates dir)
  24. * @param array $vars assigned variables
  25. */
  26. public function __construct($file, $vars = false)
  27. {
  28. $this->file = $file;
  29. if (!file_exists($this->file))
  30. {
  31. throw new \Exception("View '{$this->file}' not found!");
  32. }
  33. if ($vars !== false)
  34. {
  35. $this->vars = $vars;
  36. }
  37. }
  38. /**
  39. * Assign specific variable to the template
  40. *
  41. * <code>
  42. * // assign single var
  43. * $view->assign('varname', 'varvalue');
  44. * // assign array of vars
  45. * $view->assign(array('varname1' => 'varvalue1', 'varname2' => 'varvalue2'));
  46. * </code>
  47. *
  48. * @param mixed $name Variable name
  49. * @param mixed $value Variable value
  50. */
  51. public function assign($name, $value = null)
  52. {
  53. if (is_array($name))
  54. {
  55. array_merge($this->vars, $name);
  56. }
  57. else
  58. {
  59. $this->vars[$name] = $value;
  60. }
  61. }
  62. /**
  63. * Return template output as string
  64. *
  65. * @return string content of compiled view template
  66. */
  67. public function render($stripUselessSpaces = false)
  68. {
  69. ob_start();
  70. extract($this->vars, EXTR_SKIP);
  71. include $this->file;
  72. $content = ob_get_clean();
  73. if($stripUselessSpaces)
  74. {
  75. $content = self::stripUselessSpace($content);
  76. }
  77. return $content;
  78. }
  79. /**
  80. * Display (echoes) the rendered template
  81. */
  82. public function display($stripUselessSpaces = false)
  83. {
  84. echo $this->render($stripUselessSpaces);
  85. }
  86. /**
  87. * Render the content and return it
  88. *
  89. * <code>
  90. * echo new View('blog', array('title' => 'My title'));
  91. * </code>
  92. *
  93. * @return string content of the view
  94. */
  95. public function __toString()
  96. {
  97. //return $this->render();
  98. ///*
  99. try
  100. {
  101. $res = $this->render();
  102. return $res;
  103. }
  104. catch(\Exception $e)
  105. {
  106. return $e->getMessage();
  107. }
  108. //*/
  109. }
  110. /**
  111. * Render action in view
  112. *
  113. * <code>
  114. * echo $view->action('blog', 'index', 'post', array($post_uid));
  115. * // alternative arguments
  116. * echo $view->action('blog/index/post', array($post_uid));
  117. * </code>
  118. *
  119. * @param string $application application name
  120. * @param string $controller controller name
  121. * @param string $action action name
  122. * @param array $params request parameters
  123. * @return string
  124. */
  125. public function action()
  126. {
  127. $params = array();
  128. $arg_v = func_get_args();
  129. $arg_c = func_num_args();
  130. if($arg_c > 2)
  131. {
  132. $application = $arg_v[0];
  133. $controller = $arg_v[1];
  134. $action = $arg_v[2];
  135. if($arg_c > 3)
  136. {
  137. $params = (array)$arg_v[3];
  138. }
  139. }
  140. else
  141. {
  142. list($application, $controller, $action) = explode('/', $arg_v[0]);
  143. if($arg_c == 2)
  144. {
  145. $params = (array)$arg_v[1];
  146. }
  147. }
  148. return \shozu\Dispatcher::render($application, $controller, $action, $params);
  149. }
  150. /**
  151. * Escape HTML special chars
  152. *
  153. * @param string
  154. * @return string
  155. */
  156. public function escape($string)
  157. {
  158. return htmlspecialchars($string);
  159. }
  160. /**
  161. * Limit string to given length but do not truncate words
  162. *
  163. * @param string $str input string
  164. * @param integer $length length limit
  165. * @param integer $minword
  166. * @return string
  167. */
  168. public function limit($str, $length, $minword = 3)
  169. {
  170. return \shozu\Inflector::limit($str, $length, $minword);
  171. }
  172. /**
  173. * Multibyte-aware ucfirst.
  174. *
  175. * Uppercase first letter
  176. *
  177. * @param string $str
  178. * @param string $e encoding, defaults to utf-8
  179. * @return string
  180. */
  181. public function ucfirst($str, $e = 'utf-8')
  182. {
  183. $fc = mb_strtoupper(mb_substr($str, 0, 1, $e), $e);
  184. return $fc . mb_substr($str, 1, mb_strlen($str, $e), $e);
  185. }
  186. /**
  187. * Translate helper
  188. *
  189. * @param string
  190. * @return string
  191. */
  192. public function T($string, $escape_html = true)
  193. {
  194. $shozu = \shozu\Shozu::getInstance();
  195. if($shozu->use_i18n)
  196. {
  197. if(isset($shozu->translations[$string]))
  198. {
  199. $string = $shozu->translations[$string];
  200. }
  201. elseif($shozu->debug)
  202. {
  203. $string = '***' . $string;
  204. }
  205. }
  206. if($escape_html)
  207. {
  208. $string = $this->escape($string);
  209. }
  210. return $string;
  211. }
  212. /**
  213. * Generate URL
  214. *
  215. * @param string $target application/controller/action
  216. * @param array $params action parameters
  217. * @param array $site website
  218. * @return string
  219. */
  220. public function url($target, array $params = null, $site = null)
  221. {
  222. return \shozu\Shozu::getInstance()->url($target, $params, $site);
  223. }
  224. private static function stripUselessSpace($html)
  225. {
  226. return preg_replace('#(?:(?:(^|>[^<]*?)[\t\s\r\n]*)|(?:[\t\s\r\n]*(<|$)))#', '$1$2', $html);
  227. }
  228. /**
  229. * Cache portions of a view. Usage:
  230. *
  231. * <code>
  232. * <?php if($this->cacheBegin('myCacheId')){ ?>
  233. * <!-- some dynamic content here will be cached for 600 seconds -->
  234. * <?php $this->cacheEnd(600);} ?>
  235. * </code>
  236. *
  237. * @param string $id
  238. * @return boolean
  239. */
  240. public function cacheBegin($id)
  241. {
  242. $cache = self::getCache();
  243. $this->cache_id = $id;
  244. if(($contentFromCache = $cache->fetch($id)) === false)
  245. {
  246. ob_start();
  247. return true;
  248. }
  249. else
  250. {
  251. echo $contentFromCache;
  252. return false;
  253. }
  254. }
  255. /**
  256. *
  257. * @param integer $ttl
  258. */
  259. public function cacheEnd($ttl = 0)
  260. {
  261. $cache = self::getCache();
  262. if(($contentFromCache = $cache->fetch($this->cache_id)) === false)
  263. {
  264. $contentToCache = ob_get_contents();
  265. $cache->store($this->cache_id, $contentToCache, $ttl);
  266. ob_end_clean();
  267. echo $contentToCache;
  268. }
  269. else
  270. {
  271. ob_end_clean();
  272. }
  273. }
  274. private static function getCache()
  275. {
  276. if(is_null(self::$cache))
  277. {
  278. self::$cache = \shozu\Cache::getInstance('view_cache', array('type' => 'array'));
  279. }
  280. return self::$cache;
  281. }
  282. public static function setCache(\shozu\Cache $cache)
  283. {
  284. self::$cache = $cache;
  285. }
  286. }