Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

205 lines
4.7 KiB

  1. <?php
  2. /**
  3. * @file src/Core/Renderer.php
  4. */
  5. namespace Friendica\Core;
  6. use Exception;
  7. use Friendica\BaseObject;
  8. use Friendica\Core\System;
  9. use Friendica\Render\FriendicaSmarty;
  10. /**
  11. * @brief This class handles Renderer related functions.
  12. */
  13. class Renderer extends BaseObject
  14. {
  15. /**
  16. * @brief An array of registered template engines ('name'=>'class name')
  17. */
  18. public static $template_engines = [];
  19. /**
  20. * @brief An array of instanced template engines ('name'=>'instance')
  21. */
  22. public static $template_engine_instance = [];
  23. /**
  24. * @brief An array for all theme-controllable parameters
  25. *
  26. * Mostly unimplemented yet. Only options 'template_engine' and
  27. * beyond are used.
  28. */
  29. public static $theme = [
  30. 'sourcename' => '',
  31. 'videowidth' => 425,
  32. 'videoheight' => 350,
  33. 'force_max_items' => 0,
  34. 'stylesheet' => '',
  35. 'template_engine' => 'smarty3',
  36. ];
  37. private static $ldelim = [
  38. 'internal' => '',
  39. 'smarty3' => '{{'
  40. ];
  41. private static $rdelim = [
  42. 'internal' => '',
  43. 'smarty3' => '}}'
  44. ];
  45. /**
  46. * @brief This is our template processor
  47. *
  48. * @param string|FriendicaSmarty $s The string requiring macro substitution or an instance of FriendicaSmarty
  49. * @param array $vars key value pairs (search => replace)
  50. *
  51. * @return string substituted string
  52. */
  53. public static function replaceMacros($s, $vars)
  54. {
  55. $stamp1 = microtime(true);
  56. $a = self::getApp();
  57. // pass $baseurl to all templates
  58. $vars['$baseurl'] = System::baseUrl();
  59. $t = self::getTemplateEngine();
  60. try {
  61. $output = $t->replaceMacros($s, $vars);
  62. } catch (Exception $e) {
  63. echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
  64. exit();
  65. }
  66. $a->saveTimestamp($stamp1, "rendering");
  67. return $output;
  68. }
  69. /**
  70. * @brief Load a given template $s
  71. *
  72. * @param string $s Template to load.
  73. * @param string $root Optional.
  74. *
  75. * @return string template.
  76. */
  77. public static function getMarkupTemplate($s, $root = '')
  78. {
  79. $stamp1 = microtime(true);
  80. $a = self::getApp();
  81. $t = self::getTemplateEngine();
  82. try {
  83. $template = $t->getTemplateFile($s, $root);
  84. } catch (Exception $e) {
  85. echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
  86. killme();
  87. }
  88. $a->saveTimestamp($stamp1, "file");
  89. return $template;
  90. }
  91. /**
  92. * @brief Register template engine class
  93. *
  94. * @param string $class
  95. */
  96. public static function registerTemplateEngine($class)
  97. {
  98. $v = get_class_vars($class);
  99. if (!empty($v['name']))
  100. {
  101. $name = $v['name'];
  102. self::$template_engines[$name] = $class;
  103. } else {
  104. echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
  105. die();
  106. }
  107. }
  108. /**
  109. * @brief Return template engine instance.
  110. *
  111. * If $name is not defined, return engine defined by theme,
  112. * or default
  113. *
  114. * @return object Template Engine instance
  115. */
  116. public static function getTemplateEngine()
  117. {
  118. $template_engine = defaults(self::$theme, 'template_engine', 'smarty3');
  119. if (isset(self::$template_engines[$template_engine])) {
  120. if (isset(self::$template_engine_instance[$template_engine])) {
  121. return self::$template_engine_instance[$template_engine];
  122. } else {
  123. $class = self::$template_engines[$template_engine];
  124. $obj = new $class;
  125. self::$template_engine_instance[$template_engine] = $obj;
  126. return $obj;
  127. }
  128. }
  129. echo "template engine <tt>$template_engine</tt> is not registered!\n";
  130. exit();
  131. }
  132. /**
  133. * @brief Returns the active template engine.
  134. *
  135. * @return string the active template engine
  136. */
  137. public static function getActiveTemplateEngine()
  138. {
  139. return self::$theme['template_engine'];
  140. }
  141. /**
  142. * sets the active template engine
  143. *
  144. * @param string $engine the template engine (default is Smarty3)
  145. */
  146. public static function setActiveTemplateEngine($engine = 'smarty3')
  147. {
  148. self::$theme['template_engine'] = $engine;
  149. }
  150. /**
  151. * Gets the right delimiter for a template engine
  152. *
  153. * Currently:
  154. * Internal = ''
  155. * Smarty3 = '{{'
  156. *
  157. * @param string $engine The template engine (default is Smarty3)
  158. *
  159. * @return string the right delimiter
  160. */
  161. public static function getTemplateLeftDelimiter($engine = 'smarty3')
  162. {
  163. return self::$ldelim[$engine];
  164. }
  165. /**
  166. * Gets the left delimiter for a template engine
  167. *
  168. * Currently:
  169. * Internal = ''
  170. * Smarty3 = '}}'
  171. *
  172. * @param string $engine The template engine (default is Smarty3)
  173. *
  174. * @return string the left delimiter
  175. */
  176. public static function getTemplateRightDelimiter($engine = 'smarty3')
  177. {
  178. return self::$rdelim[$engine];
  179. }
  180. }