template.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Copyright (C) 2013
  17. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //define the template class
  22. if (!class_exists('template')) {
  23. class template {
  24. public $engine;
  25. public $template_dir;
  26. public $cache_dir;
  27. private $object;
  28. private $var_array;
  29. public function __construct(){
  30. }
  31. public function init() {
  32. if ($this->engine === 'smarty') {
  33. require_once "resources/templates/engine/smarty/Smarty.class.php";
  34. $this->object = new Smarty();
  35. $this->object->setTemplateDir($this->template_dir);
  36. $this->object->setCompileDir($this->cache_dir);
  37. $this->object->setCacheDir($this->cache_dir);
  38. $this->object->registerPlugin("modifier","in_array", "in_array");
  39. }
  40. if ($this->engine === 'raintpl') {
  41. require_once "resources/templates/engine/raintpl/rain.tpl.class.php";
  42. $this->object = new RainTPL();
  43. RainTPL::configure('tpl_dir', realpath($this->template_dir)."/");
  44. RainTPL::configure('cache_dir', realpath($this->cache_dir)."/");
  45. }
  46. if ($this->engine === 'twig') {
  47. require_once "resources/templates/engine/Twig/Autoloader.php";
  48. Twig_Autoloader::register();
  49. $loader = new Twig_Loader_Filesystem($this->template_dir);
  50. $this->object = new Twig_Environment($loader);
  51. $lexer = new Twig_Lexer($this->object, array(
  52. 'tag_comment' => array('{*', '*}'),
  53. 'tag_block' => array('{', '}'),
  54. 'tag_variable' => array('{$', '}'),
  55. ));
  56. $this->object->setLexer($lexer);
  57. }
  58. }
  59. public function assign($key, $value) {
  60. if ($this->engine === 'smarty') {
  61. $this->object->assign($key, $value);
  62. }
  63. if ($this->engine === 'raintpl') {
  64. $this->object->assign($key, $value);
  65. }
  66. if ($this->engine === 'twig') {
  67. $this->var_array[$key] = $value;
  68. }
  69. }
  70. public function render($name) {
  71. if ($this->engine === 'smarty') {
  72. return $this->object->fetch($name);
  73. }
  74. if ($this->engine === 'raintpl') {
  75. return $this->object-> draw($name, 'return_string=true');
  76. }
  77. if ($this->engine === 'twig') {
  78. return $this->object->render($name,$this->var_array);
  79. }
  80. }
  81. }
  82. }
  83. ?>