HandlebarMustache.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * HandlebarMustache class.
  4. *
  5. * This is an extended Mustache class which contains file handling for templates
  6. * and partial templates.
  7. *
  8. * @extends Mustache
  9. */
  10. class HandlebarMustache extends Mustache {
  11. /**
  12. * templateBase directory.
  13. *
  14. * If none is specified, this will default to `dirname(__FILE__)`.
  15. *
  16. * @var string
  17. * @access protected
  18. */
  19. protected $templateBase;
  20. /**
  21. * templateName.
  22. *
  23. * If none is specified, this will default to an underscorified version of the class name.
  24. *
  25. * @var string
  26. * @access protected
  27. */
  28. protected $templateName;
  29. /**
  30. * HandlebarMustache class constructor.
  31. *
  32. * @access public
  33. * @param string $template (default: null)
  34. * @param mixed $view (default: null)
  35. * @param array $partials (default: null)
  36. * @return void
  37. */
  38. public function __construct($template = null, $view = null, $partials = null) {
  39. parent::__construct($template,$view,$partials);
  40. // default template base is the current directory.
  41. if (!isset($this->templateBase)) {
  42. $this->setTemplateBase(dirname(__FILE__));
  43. }
  44. // default template name is the underscorified class name.
  45. if (!isset($this->templateName)) {
  46. $this->templateName = strtolower(preg_replace('#(?<!^)([A-Z]+)#', '_\1', get_class($this)));
  47. }
  48. }
  49. /**
  50. * Override the current templateBase.
  51. *
  52. * @access public
  53. * @param string $dir
  54. * @return void
  55. */
  56. public function setTemplateBase($dir) {
  57. if (substr($dir, -1) !== '/') {
  58. $dir .= '/';
  59. }
  60. $this->templateBase = $dir;
  61. }
  62. /**
  63. * Override the default templateName.
  64. *
  65. * @access public
  66. * @param string $name
  67. * @return void
  68. */
  69. public function setTemplateName($name) {
  70. $this->templateName = $name;
  71. }
  72. /**
  73. * Load a template file. This file will be relative to $this->templateBase.
  74. * A '.mustache' file extension is assumed if none is provided in $file.
  75. *
  76. * @access public
  77. * @param string $name
  78. * @return void
  79. */
  80. public function loadTemplate($name) {
  81. if (strpos($name, '.') === false) {
  82. $name .= '.mustache';
  83. }
  84. $filename = $this->templateBase . $name;
  85. if (file_exists($filename)) {
  86. $this->template = file_get_contents($filename);
  87. } else {
  88. $this->template = null;
  89. }
  90. }
  91. /**
  92. * Load a partial, either from $this->partials or from a file in the templateBase
  93. * directory.
  94. *
  95. * @access protected
  96. * @param string $tag_name
  97. * @return string Partial template.
  98. */
  99. protected function getPartial($tag_name) {
  100. try {
  101. if ($result = parent::getPartial($tag_name)) {
  102. return $result;
  103. }
  104. } catch (MustacheException $e) {
  105. // Ignore the UNKNOWN_PARTIAL exceptions, we'll just look for a template file.
  106. if ($e->getCode() !== MustacheException::UNKNOWN_PARTIAL) {
  107. throw $e;
  108. }
  109. }
  110. $filename = $this->templateBase . $tag_name . '.mustache';
  111. if (file_exists($filename)) {
  112. $this->partials[$tag_name] = file_get_contents($filename);
  113. return $this->partials[$tag_name];
  114. } else {
  115. if ($this->throwPartialExceptions) {
  116. throw new MustacheException(
  117. 'Unknown partial: ' . $tag_name,
  118. MustacheException::UNKNOWN_PARTIAL
  119. );
  120. } else {
  121. return '';
  122. }
  123. }
  124. }
  125. /**
  126. * Render the given template and view object.
  127. *
  128. * Defaults to the template and view passed to the class constructor unless a new one is provided.
  129. * Optionally, pass an associative array of partials as well.
  130. *
  131. * @access public
  132. * @param string $template (default: null)
  133. * @param mixed $view (default: null)
  134. * @param array $partials (default: null)
  135. * @return string Rendered Mustache template.
  136. */
  137. public function render($template = null, $view = null, $partials = null) {
  138. if ($template === null && !isset($this->template)) {
  139. $this->loadTemplate($this->templateName);
  140. }
  141. return parent::render($template, $view, $partials);
  142. }
  143. }