Template.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * This file is part of the Text_Template package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * A simple template engine.
  12. *
  13. * @since Class available since Release 1.0.0
  14. */
  15. class Text_Template
  16. {
  17. /**
  18. * @var string
  19. */
  20. protected $template = '';
  21. /**
  22. * @var string
  23. */
  24. protected $openDelimiter = '{';
  25. /**
  26. * @var string
  27. */
  28. protected $closeDelimiter = '}';
  29. /**
  30. * @var array
  31. */
  32. protected $values = array();
  33. /**
  34. * Constructor.
  35. *
  36. * @param string $file
  37. * @throws InvalidArgumentException
  38. */
  39. public function __construct($file = '', $openDelimiter = '{', $closeDelimiter = '}')
  40. {
  41. $this->setFile($file);
  42. $this->openDelimiter = $openDelimiter;
  43. $this->closeDelimiter = $closeDelimiter;
  44. }
  45. /**
  46. * Sets the template file.
  47. *
  48. * @param string $file
  49. * @throws InvalidArgumentException
  50. */
  51. public function setFile($file)
  52. {
  53. $distFile = $file . '.dist';
  54. if (file_exists($file)) {
  55. $this->template = file_get_contents($file);
  56. }
  57. else if (file_exists($distFile)) {
  58. $this->template = file_get_contents($distFile);
  59. }
  60. else {
  61. throw new InvalidArgumentException(
  62. 'Template file could not be loaded.'
  63. );
  64. }
  65. }
  66. /**
  67. * Sets one or more template variables.
  68. *
  69. * @param array $values
  70. * @param bool $merge
  71. */
  72. public function setVar(array $values, $merge = TRUE)
  73. {
  74. if (!$merge || empty($this->values)) {
  75. $this->values = $values;
  76. } else {
  77. $this->values = array_merge($this->values, $values);
  78. }
  79. }
  80. /**
  81. * Renders the template and returns the result.
  82. *
  83. * @return string
  84. */
  85. public function render()
  86. {
  87. $keys = array();
  88. foreach ($this->values as $key => $value) {
  89. $keys[] = $this->openDelimiter . $key . $this->closeDelimiter;
  90. }
  91. return str_replace($keys, $this->values, $this->template);
  92. }
  93. /**
  94. * Renders the template and writes the result to a file.
  95. *
  96. * @param string $target
  97. */
  98. public function renderTo($target)
  99. {
  100. $fp = @fopen($target, 'wt');
  101. if ($fp) {
  102. fwrite($fp, $this->render());
  103. fclose($fp);
  104. } else {
  105. $error = error_get_last();
  106. throw new RuntimeException(
  107. sprintf(
  108. 'Could not write to %s: %s',
  109. $target,
  110. substr(
  111. $error['message'],
  112. strpos($error['message'], ':') + 2
  113. )
  114. )
  115. );
  116. }
  117. }
  118. }