PageRenderTime 63ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/doubleface/jelix-jpu
PHP | 366 lines | 167 code | 33 blank | 166 comment | 20 complexity | c6987a3b89b9017b935a7e85147a52ee 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-2008 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. $this->_vars = array_merge($this->_vars, $name);
  58. }else{
  59. $this->_vars[$name] = $value;
  60. }
  61. }
  62. /**
  63. * assign a value by reference in a template variable
  64. * @param string $name the variable name
  65. * @param mixed $value the value
  66. * @since jelix 1.1
  67. */
  68. public function assignByRef ($name, & $value){
  69. $this->_vars[$name] = &$value;
  70. }
  71. /**
  72. * concat a value in with a value of an existing template variable
  73. * @param string|array $name the variable name, or an associative array 'name'=>'value'
  74. * @param mixed $value the value (or null if $name is an array)
  75. */
  76. public function append ($name, $value = null){
  77. if(is_array($name)){
  78. foreach ($name as $key => $val) {
  79. if(isset($this->_vars[$key]))
  80. $this->_vars[$key] .= $val;
  81. else
  82. $this->_vars[$key] = $val;
  83. }
  84. }else{
  85. if(isset($this->_vars[$name]))
  86. $this->_vars[$name] .= $value;
  87. else
  88. $this->_vars[$name] = $value;
  89. }
  90. }
  91. /**
  92. * assign a value in a template variable, only if the template variable doesn't exist
  93. * @param string|array $name the variable name, or an associative array 'name'=>'value'
  94. * @param mixed $value the value (or null if $name is an array)
  95. */
  96. public function assignIfNone ($name, $value = null){
  97. if(is_array($name)){
  98. foreach ($name as $key => $val) {
  99. if(!isset($this->_vars[$key]))
  100. $this->_vars[$key] = $val;
  101. }
  102. }else{
  103. if(!isset($this->_vars[$name]))
  104. $this->_vars[$name] = $value;
  105. }
  106. }
  107. #ifnot JTPL_STANDALONE
  108. /**
  109. * assign a zone content to a template variable
  110. * @param string $name the variable name
  111. * @param string $zoneName a zone selector
  112. * @param array $params parameters for the zone
  113. * @see jZone
  114. */
  115. function assignZone($name, $zoneName, $params=array()){
  116. $this->_vars[$name] = jZone::get ($zoneName, $params);
  117. }
  118. /**
  119. * append a zone content to a template variable
  120. * @param string $name the variable name
  121. * @param string $zoneName a zone selector
  122. * @param array $params parameters for the zone
  123. * @see jZone
  124. * @since 1.0
  125. */
  126. function appendZone($name, $zoneName, $params=array()){
  127. if(isset($this->_vars[$name]))
  128. $this->_vars[$name] .= jZone::get ($zoneName, $params);
  129. else
  130. $this->_vars[$name] = jZone::get ($zoneName, $params);
  131. }
  132. /**
  133. * assign a zone content to a template variable only if this variable doesn't exist
  134. * @param string $name the variable name
  135. * @param string $zoneName a zone selector
  136. * @param array $params parameters for the zone
  137. * @see jZone
  138. */
  139. function assignZoneIfNone($name, $zoneName, $params=array()){
  140. if(!isset($this->_vars[$name]))
  141. $this->_vars[$name] = jZone::get ($zoneName, $params);
  142. }
  143. #endif
  144. /**
  145. * says if a template variable exists
  146. * @param string $name the variable template name
  147. * @return boolean true if the variable exists
  148. */
  149. public function isAssigned ($name){
  150. return isset ($this->_vars[$name]);
  151. }
  152. /**
  153. * return the value of a template variable
  154. * @param string $name the variable template name
  155. * @return mixed the value (or null if it isn't exist)
  156. */
  157. public function get ($name){
  158. if (isset ($this->_vars[$name])){
  159. return $this->_vars[$name];
  160. }else{
  161. $return = null;
  162. return $return;
  163. }
  164. }
  165. /**
  166. * Return all template variables
  167. * @return array
  168. */
  169. public function getTemplateVars (){
  170. return $this->_vars;
  171. }
  172. /**
  173. * process all meta instruction of a template
  174. * @param string $tpl template selector
  175. * @param string $outputtype the type of output (html, text etc..)
  176. * @param boolean $trusted says if the template file is trusted or not
  177. */
  178. public function meta($tpl, $outputtype='', $trusted = true){
  179. $this->getTemplate($tpl,'template_meta_', $outputtype, $trusted);
  180. return $this->_meta;
  181. }
  182. /**
  183. * display the generated content from the given template
  184. * @param string $tpl template selector
  185. * @param string $outputtype the type of output (html, text etc..)
  186. * @param boolean $trusted says if the template file is trusted or not
  187. */
  188. public function display ($tpl, $outputtype='', $trusted = true){
  189. $this->getTemplate($tpl,'template_', $outputtype, $trusted);
  190. }
  191. /**
  192. * contains the name of the template file
  193. * It have a public access only for plugins. So you musn't use directly this property
  194. * except from tpl plugins.
  195. * @var string
  196. * @since 1.1
  197. */
  198. public $_templateName;
  199. /**
  200. * include the compiled template file and call one of the generated function
  201. * @param string $tpl template selector
  202. * @param string $fctname the internal function name (meta or content)
  203. * @param string $outputtype the type of output (html, text etc..)
  204. * @param boolean $trusted says if the template file is trusted or not
  205. */
  206. protected function getTemplate($tpl,$fctname, $outputtype='', $trusted = true){
  207. #ifnot JTPL_STANDALONE
  208. $sel = new jSelectorTpl($tpl,$outputtype,$trusted);
  209. $sel->userModifiers = $this->userModifiers;
  210. $sel->userFunctions = $this->userFunctions;
  211. jIncluder::inc($sel);
  212. $this->_templateName = $sel->toString();
  213. $fct = $fctname.md5($sel->module.'_'.$sel->resource.'_'.$sel->outputType.($trusted?'_t':''));
  214. #else
  215. $this->_templateName = $tpl;
  216. $tpl = jTplConfig::$templatePath . $tpl;
  217. if ($outputtype=='')
  218. $outputtype = 'html';
  219. $cachefile = jTplConfig::$cachePath.dirname($this->_templateName).'/'.$outputtype.($trusted?'_t':'').'_'.basename($tpl);
  220. $mustCompile = jTplConfig::$compilationForce || !file_exists($cachefile);
  221. if (!$mustCompile) {
  222. if (filemtime($tpl) > filemtime($cachefile)) {
  223. $mustCompile = true;
  224. }
  225. }
  226. if ($mustCompile) {
  227. include_once(JTPL_PATH . 'jTplCompiler.class.php');
  228. $compiler = new jTplCompiler();
  229. $compiler->compile($this->_templateName,$tpl,$outputtype, $trusted, $this->userModifiers, $this->userFunctions);
  230. }
  231. require_once($cachefile);
  232. $fct = $fctname.md5($tpl.'_'.$outputtype.($trusted?'_t':''));
  233. #endif
  234. $fct($this);
  235. }
  236. /**
  237. * return the generated content from the given template
  238. * @param string $tpl template selector
  239. * @param string $outputtype the type of output (html, text etc..)
  240. * @param boolean $trusted says if the template file is trusted or not
  241. * @param boolean $callMeta false if meta should not be called
  242. * @return string the generated content
  243. */
  244. public function fetch ($tpl, $outputtype='', $trusted = true, $callMeta=true){
  245. $content = '';
  246. ob_start ();
  247. try{
  248. #ifnot JTPL_STANDALONE
  249. $sel = new jSelectorTpl($tpl, $outputtype, $trusted);
  250. $sel->userModifiers = $this->userModifiers;
  251. $sel->userFunctions = $this->userFunctions;
  252. jIncluder::inc($sel);
  253. $md = md5($sel->module.'_'.$sel->resource.'_'.$sel->outputType.($trusted?'_t':''));
  254. $this->_templateName = $sel->toString();
  255. #else
  256. $this->_templateName = $tpl;
  257. $tpl = jTplConfig::$templatePath . $tpl;
  258. $cachefile = jTplConfig::$cachePath.dirname($this->_templateName).'/'.$outputtype.($trusted?'_t':'').'_'.basename($tpl);
  259. $mustCompile = jTplConfig::$compilationForce || !file_exists($cachefile);
  260. if (!$mustCompile) {
  261. if (filemtime($tpl) > filemtime($cachefile)) {
  262. $mustCompile = true;
  263. }
  264. }
  265. if ($mustCompile) {
  266. include_once(JTPL_PATH . 'jTplCompiler.class.php');
  267. $compiler = new jTplCompiler();
  268. $compiler->compile($this->_templateName, $tpl, $outputtype, $trusted, $this->userModifiers, $this->userFunctions);
  269. }
  270. require_once($cachefile);
  271. $md = md5($tpl.'_'.$outputtype.($trusted?'_t':''));
  272. #endif
  273. if ($callMeta) {
  274. $fct = 'template_meta_'.$md;
  275. $fct($this);
  276. }
  277. $fct = 'template_'.$md;
  278. $fct($this);
  279. $content = ob_get_clean();
  280. }catch(Exception $e){
  281. ob_end_clean();
  282. throw $e;
  283. }
  284. return $content;
  285. }
  286. /**
  287. * deprecated function: optimized version of meta() + fetch().
  288. * Instead use fetch with true as $callMeta parameter.
  289. * @param string $tpl template selector
  290. * @param string $outputtype the type of output (html, text etc..)
  291. * @param boolean $trusted says if the template file is trusted or not
  292. * @return string the generated content
  293. * @deprecated
  294. */
  295. public function metaFetch ($tpl, $outputtype='', $trusted = true){
  296. return $this->fetch ($tpl, $outputtype, $trusted,true);
  297. }
  298. protected $userModifiers = array();
  299. /**
  300. * register a user modifier. The function should accept at least a
  301. * string as first parameter, and should return this string
  302. * which can be modified.
  303. * @param string $name the name of the modifier in a template
  304. * @param string $functionName the corresponding PHP function
  305. * @since jelix 1.1
  306. */
  307. public function registerModifier($name, $functionName) {
  308. $this->userModifiers[$name] = $functionName;
  309. }
  310. protected $userFunctions = array();
  311. /**
  312. * register a user function. The function should accept a jTpl object
  313. * as first parameter.
  314. * @param string $name the name of the modifier in a template
  315. * @param string $functionName the corresponding PHP function
  316. * @since jelix 1.1
  317. */
  318. public function registerFunction($name, $functionName) {
  319. $this->userFunctions[$name] = $functionName;
  320. }
  321. /**
  322. * return the current encoding
  323. * @return string the charset string
  324. * @since 1.0b2
  325. */
  326. public static function getEncoding (){
  327. #if JTPL_STANDALONE
  328. return jTplConfig::$charset;
  329. #else
  330. return $GLOBALS['gJConfig']->charset;
  331. #endif
  332. }
  333. }