HandlebarMustache.php 3.9 KB

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