PageRenderTime 28ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/views/phptal.php

http://github.com/nojimage/CakePHP-TALTAL
PHP | 231 lines | 140 code | 26 blank | 65 comment | 29 complexity | 9afc1fb23f02e57908e03509636d9791 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * PHPTAL View
  4. *
  5. * CakePHP 1.3+
  6. * PHP 5.2+
  7. *
  8. * Copyright 2011, nojimage (http://php-tips.com/)
  9. *
  10. * @filesource
  11. * @version 0.3.1
  12. * @author nojimage <nojimage at gmail.com>
  13. * @copyright 2011 nojimage (http://php-tips.com/)
  14. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  15. * @link http://php-tips.com/
  16. * @package taltal
  17. * @subpackage taltal.views
  18. * @since File available since Release 0.1
  19. */
  20. if (!class_exists('ThemeView')) {
  21. App::import('View', 'Theme');
  22. }
  23. if (!class_exists('PHPTAL')) {
  24. App::import('Vendor', 'Taltal.PHPTAL', array('file' => 'phptal' . DS . 'PHPTAL.php'));
  25. }
  26. include_once dirname(dirname(__FILE__)) . DS . 'libs' . DS . 'PHPTAL_Namespace_Cake.php';
  27. /**
  28. * PHPTALView
  29. *
  30. * @property PHPTAL $Phptal
  31. */
  32. class PhptalView extends ThemeView {
  33. /**
  34. * @var PHPTAL_Namespace_Cake
  35. */
  36. protected $_namespaceCake;
  37. /**
  38. * PHPTALView constructor
  39. *
  40. * @param Controller $controller
  41. */
  42. function __construct($controller) {
  43. parent::__construct($controller);
  44. $this->Phptal = new PHPTAL();
  45. $this->Phptal->setEncoding(Configure::read('App.encoding'));
  46. $this->Phptal->setPhpCodeDestination(CACHE . 'views');
  47. $this->_createUrlModifier();
  48. $this->_registerNamespace();
  49. }
  50. /**
  51. * Renders and returns output for given view filename with its
  52. * array of data.
  53. *
  54. * @param string $___viewFn Filename of the view
  55. * @param array $___dataForView Data to include in rendered view
  56. * @param boolean $loadHelpers Boolean to indicate that helpers should be loaded.
  57. * @param boolean $cached Whether or not to trigger the creation of a cache file.
  58. * @return string Rendered output
  59. * @access protected
  60. */
  61. function _render($___viewFn, $___dataForView, $loadHelpers = true, $cached = false) {
  62. if (!preg_match('/(?:\.zpt|\.xhtml|\.html)$/', $___viewFn)) {
  63. return parent::_render($___viewFn, $___dataForView, $loadHelpers, $cached);
  64. }
  65. $loadedHelpers = array();
  66. if ($this->helpers != false && $loadHelpers === true) {
  67. $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
  68. $helpers = array_keys($loadedHelpers);
  69. $helperNames = array_map(array('Inflector', 'variable'), $helpers);
  70. for ($i = count($helpers) - 1; $i >= 0; $i--) {
  71. $name = $helperNames[$i];
  72. $helper = $loadedHelpers[$helpers[$i]];
  73. if (!isset($___dataForView[$name])) {
  74. ${$name} = $helper;
  75. }
  76. $this->loaded[$helperNames[$i]] = $helper;
  77. $this->{$helpers[$i]} = $helper;
  78. }
  79. $this->_triggerHelpers('beforeRender');
  80. unset($name, $loadedHelpers, $helpers, $i, $helperNames, $helper);
  81. }
  82. // -- set template
  83. $this->Phptal->setTemplate($___viewFn);
  84. // -- set values
  85. foreach ($___dataForView as $key => $value) {
  86. $this->Phptal->set($key, $value);
  87. }
  88. // set helpers
  89. foreach ($this->loaded as $helperName => $helper) {
  90. $this->Phptal->set($helperName, $helper);
  91. $this->_createHelperModifier($helperName);
  92. }
  93. // set this View class
  94. $this->Phptal->set('view', $this);
  95. // -- render
  96. ob_start();
  97. try {
  98. echo $this->Phptal->execute();
  99. } catch (Exception $e) {
  100. debug($e->__toString());
  101. }
  102. if ($loadHelpers === true) {
  103. $this->_triggerHelpers('afterRender');
  104. }
  105. $out = ob_get_clean();
  106. $caching = (
  107. isset($this->loaded['cache']) &&
  108. (($this->cacheAction != false)) && (Configure::read('Cache.check') === true)
  109. );
  110. if ($caching) {
  111. if (is_a($this->loaded['cache'], 'CacheHelper')) {
  112. $cache = $this->loaded['cache'];
  113. $cache->base = $this->base;
  114. $cache->here = $this->here;
  115. $cache->helpers = $this->helpers;
  116. $cache->action = $this->action;
  117. $cache->controllerName = $this->name;
  118. $cache->layout = $this->layout;
  119. $cache->cacheAction = $this->cacheAction;
  120. $cache->viewVars = $this->viewVars;
  121. $cache->cache($___viewFn, $out, $cached);
  122. }
  123. }
  124. return $out;
  125. }
  126. /**
  127. * Get the extensions that view files can use.
  128. *
  129. * @return array Array of extensions view files use.
  130. * @access protected
  131. */
  132. function _getExtensions() {
  133. $exts = parent::_getExtensions();
  134. if ($this->ext !== '.html') {
  135. array_unshift($exts, '.html');
  136. }
  137. if ($this->ext !== '.xhtml') {
  138. array_unshift($exts, '.xhtml');
  139. }
  140. if ($this->ext !== '.zpt') {
  141. array_unshift($exts, '.zpt');
  142. }
  143. return $exts;
  144. }
  145. /**
  146. * create helper modifier
  147. *
  148. * @param string $helperName
  149. */
  150. protected function _createHelperModifier($helperName) {
  151. $functionName = 'phptal_tales_' . Inflector::underscore($helperName);
  152. $helperName = Inflector::camelize($helperName);
  153. if (!function_exists($functionName)) {
  154. $func = "function {$functionName}(\$src, \$nothrow) {
  155. \$src = trim(\$src);
  156. \$src = 'php:view.{$helperName}.' . \$src;
  157. return phptal_tales(\$src, \$nothrow);
  158. }";
  159. eval($func);
  160. }
  161. }
  162. /**
  163. * create url modifier
  164. */
  165. protected function _createUrlModifier() {
  166. if (!function_exists('phptal_tales_url')) {
  167. function phptal_tales_url($src, $nothrow) {
  168. $src = trim($src);
  169. if (preg_match('/^[a-z0-9_]+:/i', $src)) {
  170. $src = phptal_tales($src, $nothrow);
  171. } else if (preg_match('/^[a-z0-9_]+\(/i', $src)) {
  172. $src = phptal_tales('php:' . $src, $nothrow);
  173. } else {
  174. $src = "'" . $src . "'";
  175. }
  176. return 'Router::url(' . $src . ')';
  177. }
  178. }
  179. if (!function_exists('phptal_tales_fullurl')) {
  180. function phptal_tales_fullurl($src, $nothrow) {
  181. $src = trim($src);
  182. if (preg_match('/^[a-z0-9_]+:/i', $src)) {
  183. $src = phptal_tales($src, $nothrow);
  184. } else if (preg_match('/^[a-z0-9_]+\(/i', $src)) {
  185. $src = phptal_tales('php:' . $src, $nothrow);
  186. } else {
  187. $src = "'" . $src . "'";
  188. }
  189. return 'Router::url(' . $src . ', true)';
  190. }
  191. }
  192. }
  193. /**
  194. * register cake namespace
  195. */
  196. protected function _registerNamespace() {
  197. $defs = PHPTAL_Dom_Defs::getInstance();
  198. /* @var $defs PHPTAL_Dom_Defs */
  199. if (isset($this->_namespaceCake) || $defs->isHandledNamespace(PHPTAL_Namespace_Cake::NAMESPACE_URI)) {
  200. return;
  201. }
  202. $this->_namespaceCake = new PHPTAL_Namespace_Cake();
  203. $defs->registerNamespace($this->_namespaceCake);
  204. }
  205. }