HandlebarMustache.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * HandlebarMustache class constructor.
  22. *
  23. * @access public
  24. * @param string $template (default: null)
  25. * @param mixed $view (default: null)
  26. * @param array $partials (default: null)
  27. * @return void
  28. */
  29. public function __construct($template = null, $view = null, $partials = null) {
  30. parent::__construct($template,$view,$partials);
  31. // default template base is the current directory.
  32. if (!isset($this->templateBase)) {
  33. $this->setTemplateBase(dirname(__FILE__));
  34. }
  35. }
  36. /**
  37. * Override the current templateBase.
  38. *
  39. * @access public
  40. * @param string $dir
  41. * @return void
  42. */
  43. public function setTemplateBase($dir) {
  44. if (substr($dir, -1) !== '/') {
  45. $dir .= '/';
  46. }
  47. $this->templateBase = $dir;
  48. }
  49. /**
  50. * Load a template file. This file will be relative to $this->templateBase.
  51. * A '.mustache' file extension is assumed if none is provided in $file.
  52. *
  53. * @access public
  54. * @param string $file
  55. * @return void
  56. */
  57. public function loadTemplate($file) {
  58. if (strpos($file, '.') === false) {
  59. $file .= '.mustache';
  60. }
  61. $filename = $this->templateBase . $file;
  62. if (file_exists($filename)) {
  63. $this->template = file_get_contents($filename);
  64. } else {
  65. $this->template = null;
  66. }
  67. }
  68. /**
  69. * Load a partial, either from $this->partials or from a file in the templateBase
  70. * directory.
  71. *
  72. * @access protected
  73. * @param string $tag_name
  74. * @return string Partial template.
  75. */
  76. protected function getPartial($tag_name) {
  77. try {
  78. if ($result = parent::getPartial($tag_name)) {
  79. return $result;
  80. }
  81. } catch (MustacheException $e) {
  82. // Ignore the UNKNOWN_PARTIAL exceptions, we'll just look for a template file.
  83. if ($e->getCode() !== MustacheException::UNKNOWN_PARTIAL) {
  84. throw $e;
  85. }
  86. }
  87. $filename = $this->templateBase . $tag_name . '.mustache';
  88. if (file_exists($filename)) {
  89. $this->partials[$tag_name] = file_get_contents($filename);
  90. return $this->partials[$tag_name];
  91. } else {
  92. if ($this->throwPartialExceptions) {
  93. throw new MustacheException(
  94. 'Unknown partial: ' . $tag_name,
  95. MustacheException::UNKNOWN_PARTIAL
  96. );
  97. } else {
  98. return '';
  99. }
  100. }
  101. }
  102. }