PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jelix/tpl/jTpl.class.php

https://bitbucket.org/jelix/jelix-1.0.x
PHP | 314 lines | 150 code | 26 blank | 138 comment | 19 complexity | 58a41e7d396e6006b6fd7ac29f77e436 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package jelix
  4. * @subpackage jtpl
  5. * @author Laurent Jouanneau
  6. * @contributor Dominique Papin
  7. * @copyright 2005-2006 Laurent Jouanneau, 2007 Dominique Papin
  8. * @link http://www.jelix.org
  9. * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  10. */
  11. /**
  12. * template engine
  13. * @package jelix
  14. * @subpackage jtpl
  15. */
  16. class jTpl {
  17. /**
  18. * all assigned template variables.
  19. * It have a public access only for plugins. So you musn't use directly this property
  20. * except from tpl plugins.
  21. * See methods of jTpl to manage template variables
  22. * @var array
  23. */
  24. public $_vars = array ();
  25. /**
  26. * temporary template variables for plugins.
  27. * It have a public access only for plugins. So you musn't use directly this property
  28. * except from tpl plugins.
  29. * @var array
  30. */
  31. public $_privateVars = array ();
  32. /**
  33. * internal use
  34. * It have a public access only for plugins. So you musn't use directly this property
  35. * except from tpl plugins.
  36. * @var array
  37. */
  38. public $_meta = array();
  39. public function __construct(){
  40. #ifnot JTPL_STANDALONE
  41. global $gJConfig;
  42. $this->_vars['j_basepath'] = $gJConfig->urlengine['basePath'];
  43. $this->_vars['j_jelixwww'] = $gJConfig->urlengine['jelixWWWPath'];
  44. $this->_vars['j_themepath'] = $gJConfig->urlengine['basePath'].'themes/'.$gJConfig->theme.'/';
  45. $this->_vars['j_enableOldActionSelector'] = $gJConfig->enableOldActionSelector;
  46. #endif
  47. $this->_vars['j_datenow'] = date('Y-m-d');
  48. $this->_vars['j_timenow'] = date('H:i:s');
  49. }
  50. /**
  51. * assign a value in a template variable
  52. * @param string|array $name the variable name, or an associative array 'name'=>'value'
  53. * @param mixed $value the value (or null if $name is an array)
  54. */
  55. public function assign ($name, $value = null){
  56. if(is_array($name)){
  57. foreach ($name as $key => $val) {
  58. $this->_vars[$key] = $val;
  59. }
  60. }else{
  61. $this->_vars[$name] = $value;
  62. }
  63. }
  64. /**
  65. * concat a value in with a value of an existing template variable
  66. * @param string|array $name the variable name, or an associative array 'name'=>'value'
  67. * @param mixed $value the value (or null if $name is an array)
  68. */
  69. public function append ($name, $value = null){
  70. if(is_array($name)){
  71. foreach ($name as $key => $val) {
  72. if(isset($this->_vars[$key]))
  73. $this->_vars[$key] .= $val;
  74. else
  75. $this->_vars[$key] = $val;
  76. }
  77. }else{
  78. if(isset($this->_vars[$name]))
  79. $this->_vars[$name] .= $value;
  80. else
  81. $this->_vars[$name] = $value;
  82. }
  83. }
  84. /**
  85. * assign a value in a template variable, only if the template variable doesn't exist
  86. * @param string|array $name the variable name, or an associative array 'name'=>'value'
  87. * @param mixed $value the value (or null if $name is an array)
  88. */
  89. public function assignIfNone ($name, $value = null){
  90. if(is_array($name)){
  91. foreach ($name as $key => $val) {
  92. if(!isset($this->_vars[$key]))
  93. $this->_vars[$key] = $val;
  94. }
  95. }else{
  96. if(!isset($this->_vars[$name]))
  97. $this->_vars[$name] = $value;
  98. }
  99. }
  100. #ifnot JTPL_STANDALONE
  101. /**
  102. * assign a zone content to a template variable
  103. * @param string $name the variable name
  104. * @param string $zoneName a zone selector
  105. * @param array $params parameters for the zone
  106. * @see jZone
  107. */
  108. function assignZone($name, $zoneName, $params=array()){
  109. $this->_vars[$name] = jZone::get ($zoneName, $params);
  110. }
  111. /**
  112. * append a zone content to a template variable
  113. * @param string $name the variable name
  114. * @param string $zoneName a zone selector
  115. * @param array $params parameters for the zone
  116. * @see jZone
  117. * @since 1.0
  118. */
  119. function appendZone($name, $zoneName, $params=array()){
  120. if(isset($this->_vars[$name]))
  121. $this->_vars[$name] .= jZone::get ($zoneName, $params);
  122. else
  123. $this->_vars[$name] = jZone::get ($zoneName, $params);
  124. }
  125. /**
  126. * assign a zone content to a template variable only if this variable doesn't exist
  127. * @param string $name the variable name
  128. * @param string $zoneName a zone selector
  129. * @param array $params parameters for the zone
  130. * @see jZone
  131. */
  132. function assignZoneIfNone($name, $zoneName, $params=array()){
  133. if(!isset($this->_vars[$name]))
  134. $this->_vars[$name] = jZone::get ($zoneName, $params);
  135. }
  136. #endif
  137. /**
  138. * says if a template variable exists
  139. * @param string $name the variable template name
  140. * @return boolean true if the variable exists
  141. */
  142. public function isAssigned ($name){
  143. return isset ($this->_vars[$name]);
  144. }
  145. /**
  146. * return the value of a template variable
  147. * @param string $name the variable template name
  148. * @return mixed the value (or null if it isn't exist)
  149. */
  150. public function get ($name){
  151. if (isset ($this->_vars[$name])){
  152. return $this->_vars[$name];
  153. }else{
  154. $return = null;
  155. return $return;
  156. }
  157. }
  158. /**
  159. * Return all template variables
  160. * @return array
  161. */
  162. public function getTemplateVars (){
  163. return $this->_vars;
  164. }
  165. /**
  166. * process all meta instruction of a template
  167. * @param string $tpl template selector
  168. * @param string $outputtype the type of output (html, text etc..)
  169. * @param boolean $trusted says if the template file is trusted or not
  170. */
  171. public function meta($tpl, $outputtype='', $trusted = true){
  172. $this->getTemplate($tpl,'template_meta_', $outputtype, $trusted);
  173. return $this->_meta;
  174. }
  175. /**
  176. * display the generated content from the given template
  177. * @param string $tpl template selector
  178. * @param string $outputtype the type of output (html, text etc..)
  179. * @param boolean $trusted says if the template file is trusted or not
  180. */
  181. public function display ($tpl, $outputtype='', $trusted = true){
  182. $this->getTemplate($tpl,'template_', $outputtype, $trusted);
  183. }
  184. /**
  185. * include the compiled template file and call one of the generated function
  186. * @param string $tpl template selector
  187. * @param string $fctname the internal function name (meta or content)
  188. * @param string $outputtype the type of output (html, text etc..)
  189. * @param boolean $trusted says if the template file is trusted or not
  190. */
  191. protected function getTemplate($tpl,$fctname, $outputtype='', $trusted = true){
  192. #ifnot JTPL_STANDALONE
  193. $sel = new jSelectorTpl($tpl,$outputtype,$trusted);
  194. jIncluder::inc($sel);
  195. $fct = $fctname.md5($sel->module.'_'.$sel->resource.'_'.$sel->outputType.($trusted?'_t':''));
  196. #else
  197. $tpl = JTPL_TEMPLATES_PATH . $tpl;
  198. $filename = basename($tpl);
  199. $cachefile = JTPL_CACHE_PATH.$outputtype.($trusted?'_t':'').'_'.$filename;
  200. $mustCompile = $GLOBALS['jTplConfig']['compilation_force']['force'] || !file_exists($cachefile);
  201. if (!$mustCompile) {
  202. if (filemtime($tpl) > filemtime($cachefile)) {
  203. $mustCompile = true;
  204. }
  205. }
  206. if ($mustCompile) {
  207. include_once(JTPL_PATH . 'jTplCompiler.class.php');
  208. $compiler = new jTplCompiler();
  209. $compiler->compile($tpl,$outputtype, $trusted);
  210. }
  211. require_once($cachefile);
  212. $fct = $fctname.md5($tpl.'_'.$outputtype.($trusted?'_t':''));
  213. #endif
  214. $fct($this);
  215. }
  216. /**
  217. * return the generated content from the given template
  218. * @param string $tpl template selector
  219. * @param string $outputtype the type of output (html, text etc..)
  220. * @param boolean $trusted says if the template file is trusted or not
  221. * @param boolean $callMeta false if meta should not be called
  222. * @return string the generated content
  223. */
  224. public function fetch ($tpl, $outputtype='', $trusted = true, $callMeta=true){
  225. $content = '';
  226. ob_start ();
  227. try{
  228. #ifnot JTPL_STANDALONE
  229. $sel = new jSelectorTpl($tpl, $outputtype, $trusted);
  230. jIncluder::inc($sel);
  231. $md = md5($sel->module.'_'.$sel->resource.'_'.$sel->outputType.($trusted?'_t':''));
  232. #else
  233. $tpl = JTPL_TEMPLATES_PATH . $tpl;
  234. $filename = basename($tpl);
  235. $cachefile = JTPL_CACHE_PATH.$outputtype.($trusted?'_t':'').'_'.$filename;
  236. $mustCompile = $GLOBALS['jTplConfig']['compilation_force']['force'] || !file_exists($cachefile);
  237. if (!$mustCompile) {
  238. if (filemtime($tpl) > filemtime($cachefile)) {
  239. $mustCompile = true;
  240. }
  241. }
  242. if ($mustCompile) {
  243. include_once(JTPL_PATH . 'jTplCompiler.class.php');
  244. $compiler = new jTplCompiler();
  245. $compiler->compile($tpl,$outputtype,$trusted);
  246. }
  247. require_once($cachefile);
  248. $md = md5($tpl.'_'.$outputtype.($trusted?'_t':''));
  249. #endif
  250. if ($callMeta) {
  251. $fct = 'template_meta_'.$md;
  252. $fct($this);
  253. }
  254. $fct = 'template_'.$md;
  255. $fct($this);
  256. $content = ob_get_clean();
  257. }catch(Exception $e){
  258. ob_end_clean();
  259. throw $e;
  260. }
  261. return $content;
  262. }
  263. /**
  264. * deprecated function: optimized version of meta() + fetch().
  265. * Instead use fetch with true as $callMeta parameter.
  266. * @param string $tpl template selector
  267. * @param string $outputtype the type of output (html, text etc..)
  268. * @param boolean $trusted says if the template file is trusted or not
  269. * @return string the generated content
  270. * @deprecated
  271. */
  272. public function metaFetch ($tpl, $outputtype='', $trusted = true){
  273. return $this->fetch ($tpl, $outputtype, $trusted,true);
  274. }
  275. /**
  276. * return the current encoding
  277. * @return string the charset string
  278. * @since 1.0b2
  279. */
  280. public static function getEncoding (){
  281. #if JTPL_STANDALONE
  282. return $GLOBALS['jTplConfig']['charset'];
  283. #else
  284. return $GLOBALS['gJConfig']->charset;
  285. #endif
  286. }
  287. }
  288. ?>