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.

206 lines
4.8 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\Render\FriendicaSmarty;
  9. use Friendica\Render\ITemplateEngine;
  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. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  53. */
  54. public static function replaceMacros($s, $vars)
  55. {
  56. $stamp1 = microtime(true);
  57. $a = self::getApp();
  58. // pass $baseurl to all templates
  59. $vars['$baseurl'] = System::baseUrl();
  60. $t = self::getTemplateEngine();
  61. try {
  62. $output = $t->replaceMacros($s, $vars);
  63. } catch (Exception $e) {
  64. echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
  65. exit();
  66. }
  67. $a->saveTimestamp($stamp1, "rendering");
  68. return $output;
  69. }
  70. /**
  71. * @brief Load a given template $s
  72. *
  73. * @param string $s Template to load.
  74. * @param string $root Optional.
  75. *
  76. * @return string template.
  77. * @throws Exception
  78. */
  79. public static function getMarkupTemplate($s, $root = '')
  80. {
  81. $stamp1 = microtime(true);
  82. $a = self::getApp();
  83. $t = self::getTemplateEngine();
  84. try {
  85. $template = $t->getTemplateFile($s, $root);
  86. } catch (Exception $e) {
  87. echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
  88. exit();
  89. }
  90. $a->saveTimestamp($stamp1, "file");
  91. return $template;
  92. }
  93. /**
  94. * @brief Register template engine class
  95. *
  96. * @param string $class
  97. */
  98. public static function registerTemplateEngine($class)
  99. {
  100. $v = get_class_vars($class);
  101. if (!empty($v['name']))
  102. {
  103. $name = $v['name'];
  104. self::$template_engines[$name] = $class;
  105. } else {
  106. echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
  107. die();
  108. }
  109. }
  110. /**
  111. * @brief Return template engine instance.
  112. *
  113. * If $name is not defined, return engine defined by theme,
  114. * or default
  115. *
  116. * @return ITemplateEngine Template Engine instance
  117. */
  118. public static function getTemplateEngine()
  119. {
  120. $template_engine = defaults(self::$theme, 'template_engine', 'smarty3');
  121. if (isset(self::$template_engines[$template_engine])) {
  122. if (isset(self::$template_engine_instance[$template_engine])) {
  123. return self::$template_engine_instance[$template_engine];
  124. } else {
  125. $class = self::$template_engines[$template_engine];
  126. $obj = new $class;
  127. self::$template_engine_instance[$template_engine] = $obj;
  128. return $obj;
  129. }
  130. }
  131. echo "template engine <tt>$template_engine</tt> is not registered!\n";
  132. exit();
  133. }
  134. /**
  135. * @brief Returns the active template engine.
  136. *
  137. * @return string the active template engine
  138. */
  139. public static function getActiveTemplateEngine()
  140. {
  141. return self::$theme['template_engine'];
  142. }
  143. /**
  144. * sets the active template engine
  145. *
  146. * @param string $engine the template engine (default is Smarty3)
  147. */
  148. public static function setActiveTemplateEngine($engine = 'smarty3')
  149. {
  150. self::$theme['template_engine'] = $engine;
  151. }
  152. /**
  153. * Gets the right delimiter for a template engine
  154. *
  155. * Currently:
  156. * Internal = ''
  157. * Smarty3 = '{{'
  158. *
  159. * @param string $engine The template engine (default is Smarty3)
  160. *
  161. * @return string the right delimiter
  162. */
  163. public static function getTemplateLeftDelimiter($engine = 'smarty3')
  164. {
  165. return self::$ldelim[$engine];
  166. }
  167. /**
  168. * Gets the left delimiter for a template engine
  169. *
  170. * Currently:
  171. * Internal = ''
  172. * Smarty3 = '}}'
  173. *
  174. * @param string $engine The template engine (default is Smarty3)
  175. *
  176. * @return string the left delimiter
  177. */
  178. public static function getTemplateRightDelimiter($engine = 'smarty3')
  179. {
  180. return self::$rdelim[$engine];
  181. }
  182. }