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.

90 lines
2.4 KiB

  1. <?php
  2. require_once "object/TemplateEngine.php";
  3. require_once("library/Smarty/libs/Smarty.class.php");
  4. require_once "include/plugin.php";
  5. define('SMARTY3_TEMPLATE_FOLDER','templates');
  6. class FriendicaSmarty extends Smarty {
  7. public $filename;
  8. function __construct() {
  9. parent::__construct();
  10. $a = get_app();
  11. $theme = current_theme();
  12. // setTemplateDir can be set to an array, which Smarty will parse in order.
  13. // The order is thus very important here
  14. $template_dirs = array('theme' => "view/theme/$theme/".SMARTY3_TEMPLATE_FOLDER."/");
  15. if( x($a->theme_info,"extends") )
  16. $template_dirs = $template_dirs + array('extends' => "view/theme/".$a->theme_info["extends"]."/".SMARTY3_TEMPLATE_FOLDER."/");
  17. $template_dirs = $template_dirs + array('base' => "view/".SMARTY3_TEMPLATE_FOLDER."/");
  18. $this->setTemplateDir($template_dirs);
  19. $this->setCompileDir('view/smarty3/compiled/');
  20. $this->setConfigDir('view/smarty3/config/');
  21. $this->setCacheDir('view/smarty3/cache/');
  22. $this->left_delimiter = $a->get_template_ldelim('smarty3');
  23. $this->right_delimiter = $a->get_template_rdelim('smarty3');
  24. // Don't report errors so verbosely
  25. $this->error_reporting = E_ALL & ~E_NOTICE;
  26. }
  27. function parsed($template = '') {
  28. if($template) {
  29. return $this->fetch('string:' . $template);
  30. }
  31. return $this->fetch('file:' . $this->filename);
  32. }
  33. }
  34. class FriendicaSmartyEngine implements ITemplateEngine {
  35. static $name ="smarty3";
  36. public function __construct(){
  37. if(!is_writable('view/smarty3/')){
  38. echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver."; killme();
  39. }
  40. }
  41. // ITemplateEngine interface
  42. public function replace_macros($s, $r) {
  43. $template = '';
  44. if(gettype($s) === 'string') {
  45. $template = $s;
  46. $s = new FriendicaSmarty();
  47. }
  48. $r['$APP'] = get_app();
  49. // "middleware": inject variables into templates
  50. $arr = array(
  51. "template"=> basename($s->filename),
  52. "vars" => $r
  53. );
  54. call_hooks("template_vars", $arr);
  55. $r = $arr['vars'];
  56. foreach($r as $key=>$value) {
  57. if($key[0] === '$') {
  58. $key = substr($key, 1);
  59. }
  60. $s->assign($key, $value);
  61. }
  62. return $s->parsed($template);
  63. }
  64. public function get_template_file($file, $root=''){
  65. $a = get_app();
  66. $template_file = get_template_file($a, SMARTY3_TEMPLATE_FOLDER.'/'.$file, $root);
  67. $template = new FriendicaSmarty();
  68. $template->filename = $template_file;
  69. return $template;
  70. }
  71. }