PageRenderTime 73ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/public/codeCore/Classes/php/Template.php

https://github.com/IAmCorbin/MooKit
PHP | 109 lines | 53 code | 1 blank | 55 comment | 4 complexity | b9ea1f13743ac3aa1b3c73a59a19a1f1 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * contains Template Class
  4. * @package MooKit
  5. */
  6. /**
  7. * A very simple PHP Template Class
  8. *
  9. * Allows for seperation of presentation logic and data gathering logic
  10. *
  11. * @author Corbin Tarrant
  12. * @author adapted from {@link http://seanhess.net/posts/simple_templating_system_in_php }
  13. * @copyright March 6th, 2010
  14. * @package MooKit
  15. */
  16. class Template {
  17. /** @var array $vars variables to xfer */
  18. var $vars;
  19. /** @var string $path path to template file */
  20. var $path;
  21. /** @var string $result result of template merge */
  22. var $result;
  23. /** @var string $parent parent template */
  24. var $parent;
  25. /**
  26. * Constructor
  27. *
  28. * @param string $path path to the template file you want to load
  29. * @param array $vars array of (key=>value) variables
  30. */
  31. public function __construct($path=false, $vars=false) {
  32. $this->vars = ($vars === false) ? array() : $vars;
  33. $this->extract($vars);
  34. $this->path($path);
  35. }
  36. /**
  37. * Magic PHP __toString
  38. *
  39. * @returns the merged template
  40. */
  41. public function __toString() {
  42. return $this->run();
  43. }
  44. /** take an array of (key=>value) and set to $this->vars */
  45. public function extract($vars) {
  46. if($vars)
  47. foreach ($vars as $property => $value)
  48. $this->vars[$property] = $value;
  49. }
  50. /** set a parent template */
  51. public function parent($parent) {
  52. $this->parent = $parent;
  53. }
  54. /** Set the path to the template */
  55. public function path($path) {
  56. $this->path = $path;
  57. }
  58. /** Magic PHP __set */
  59. public function __set($name, $value) {
  60. $this->vars[$name] = $value;
  61. }
  62. /** Magic PHP __get
  63. * @returns array|string
  64. */
  65. public function __get($name) {
  66. return isset($this->vars[$name]) ? $this->vars[$name] : "";
  67. }
  68. /**
  69. * Merge a parent template's variables to this template's scope
  70. * @returns array if this template has a parent this returns the merged $vars arrays, otherwise it returns this templates $vars
  71. */
  72. public function mergevars() {
  73. if (isset($this->parent))
  74. return array_merge($this->parent->mergevars(), $this->vars);
  75. else
  76. return $this->vars;
  77. }
  78. /**
  79. * The Main Event
  80. *
  81. * Merge the vars with the template {and encode }
  82. * @param bool $gzip set gzip encoding on/off
  83. * @returns the results of the var and template merge
  84. */
  85. public function run($gzip=false) {
  86. //start output buffering
  87. ob_start();
  88. //merge variables recursively and set to current scope
  89. extract ($this->mergevars());
  90. //include the template file now that variables are setup in this scope
  91. include $this->path;
  92. //store the result of the template file merged with data
  93. $this->result = ob_get_contents();
  94. //Clean (erase) the output buffer and turn off output buffering
  95. ob_end_clean();
  96. //if gzip encoding requested, attempt to encode now
  97. if($gzip === true) {
  98. //gzip encode if browser supports
  99. if(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) {
  100. $this->result = gzencode($this->result);
  101. header('Content-Encoding: gzip');
  102. }
  103. }
  104. //return the results of the merge for this template
  105. return $this->result;
  106. }
  107. }
  108. ?>