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

/views/smarty.php

https://github.com/sevkialacatli/CakeSmarty
PHP | 173 lines | 133 code | 17 blank | 23 comment | 5 complexity | e1f53f8076a7940cb298bcbff2f255ba MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Methods for displaying presentation data
  5. *
  6. *
  7. * PHP versions 5
  8. *
  9. * CakePHP : Rapid Development Framework <http://www.cakephp.org/>
  10. * Copyright (c) 2005, Cake Software Foundation, Inc.
  11. * 1785 E. Sahara Avenue, Suite 490-204
  12. * Las Vegas, Nevada 89104
  13. *
  14. * Licensed under The MIT License
  15. * Redistributions of files must retain the above copyright notice.
  16. *
  17. * @filesource
  18. * @package cake
  19. * @subpackage cake.app.views
  20. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  21. /**
  22. * Include Smarty. By default expects it at ( VENDORS.'smarty'.DS.'Smarty.class.php' )
  23. */
  24. App::import('Vendor', 'Smarty.Smarty', array('file' => 'libs'.DS.'Smarty.class.php'));
  25. App::import('View', 'Theme');
  26. class CakeSmarty extends Smarty {
  27. public $view;
  28. private $plugin_vars = array();
  29. public function pushPluginVar($name, $value) {
  30. if (empty($this->plugin_vars[$name])) {
  31. $this->plugin_vars[$name] = array();
  32. }
  33. $this->plugin_vars[$name][] = $value;
  34. }
  35. public function popPluginVar($name) {
  36. if (!empty($this->plugin_vars[$name])) {
  37. return array_pop($this->plugin_vars[$name]);
  38. }
  39. }
  40. public function fetchVar(&$params, $name /* , $name2 */) {
  41. $args = func_get_args();
  42. foreach (array_slice($args, 1) as $name) {
  43. if (isset($params[$name])) {
  44. $var = $params[$name];
  45. unset($params[$name]);
  46. return $var;
  47. }
  48. }
  49. return null;
  50. }
  51. public function fixHtmlAttributes(&$params) {
  52. foreach (array_keys($params) as $key) {
  53. if (strpos($key, '_') !== false) {
  54. $params[str_replace('_', '-', $key)] = $params[$key];
  55. unset($params[$key]);
  56. }
  57. }
  58. }
  59. public function viewHelper($command, $name) {
  60. if (empty($this->view->loaded[$name])) {
  61. $name = ucfirst($name);
  62. trigger_error("{$command} command requires $name helper.", E_USER_ERROR);
  63. }
  64. return $this->view->loaded[$name];
  65. }
  66. }
  67. class SmartyView extends ThemeView {
  68. static public function smarty($view = null) {
  69. $smarty = new CakeSmarty();
  70. $smarty->view = $view;
  71. $smarty->plugins_dir[] = VIEWS.'plugins'.DS;
  72. $smarty->plugins_dir[] = dirname(__FILE__). DS. 'plugins'.DS;
  73. $smarty->compile_dir = TMP.'smarty'.DS.'compile'.DS;
  74. $smarty->cache_dir = TMP.'smarty'.DS.'cache'.DS;
  75. $smarty->template_dir = VIEWS.DS;
  76. $smarty->config_dir = VIEWS. DS. 'config'.DS;
  77. $smarty->cache = false;
  78. try {
  79. $smarty->configLoad('default.ini');
  80. } catch (SmartyException $e) {
  81. $obj = new Object();
  82. $obj->log("can't find 'default.ini'", LOG_DEBUG);
  83. // ignore
  84. }
  85. return $smarty;
  86. }
  87. /**
  88. * SmartyView constructor
  89. *
  90. * @param $controller instance of calling controller
  91. */
  92. function __construct (&$controller) {
  93. parent::__construct($controller);
  94. if ($this->ext == '.ctp') {
  95. $this->ext = '.html';
  96. }
  97. }
  98. /**
  99. * Renders and returns output for given view filename with its
  100. * array of data. If viewFilename has extension .ctp, then it delegates
  101. * the rendering to parent.
  102. *
  103. * @param string $___viewFn Filename of the view
  104. * @param array $___dataForView Data to include in rendered view
  105. * @param boolean $loadHelpers Boolean to indicate that helpers should be loaded.
  106. * @param boolean $cached Whether or not to trigger the creation of a cache file.
  107. * @return string Rendered output
  108. * @access protected
  109. */
  110. function _render($___viewFn, $___data_for_view, $loadHelpers = true, $cached = false) {
  111. $ext = pathinfo($___viewFn, PATHINFO_EXTENSION);
  112. if ($ext == 'ctp') {
  113. return parent::_render($___viewFn, $___data_for_view, $loadHelpers, $cached);
  114. }
  115. if ($this->helpers != false and $loadHelpers === true) {
  116. $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
  117. $helpers = array_keys($loadedHelpers);
  118. foreach ($loadedHelpers as $name => $helper) {
  119. $helperName = Inflector::variable($name);
  120. $this->loaded[$helperName] = $helper;
  121. $this->{$name} = $helper;
  122. }
  123. $this->_triggerHelpers('beforeRender');
  124. }
  125. $smarty = SmartyView::smarty($this);
  126. foreach ($this->loaded as $name => $helper) {
  127. $smarty->assign($name, $helper);
  128. }
  129. foreach($___data_for_view as $data => $value) {
  130. if (!is_object($data)) {
  131. $smarty->assign($data, $value);
  132. }
  133. }
  134. $smarty->assignByRef('view', $this);
  135. $smarty->cache = $cached;
  136. $out = $smarty->fetch($___viewFn);
  137. if ($loadHelpers === true) {
  138. $this->_triggerHelpers('afterRender');
  139. }
  140. return $out;
  141. }
  142. }