Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: /** Chris@0: * A simple template engine. Chris@0: * Chris@0: * @since Class available since Release 1.0.0 Chris@0: */ Chris@0: class Text_Template Chris@0: { Chris@0: /** Chris@0: * @var string Chris@0: */ Chris@0: protected $template = ''; Chris@0: Chris@0: /** Chris@0: * @var string Chris@0: */ Chris@0: protected $openDelimiter = '{'; Chris@0: Chris@0: /** Chris@0: * @var string Chris@0: */ Chris@0: protected $closeDelimiter = '}'; Chris@0: Chris@0: /** Chris@0: * @var array Chris@0: */ Chris@0: protected $values = array(); Chris@0: Chris@0: /** Chris@0: * Constructor. Chris@0: * Chris@0: * @param string $file Chris@0: * @throws InvalidArgumentException Chris@0: */ Chris@0: public function __construct($file = '', $openDelimiter = '{', $closeDelimiter = '}') Chris@0: { Chris@0: $this->setFile($file); Chris@0: $this->openDelimiter = $openDelimiter; Chris@0: $this->closeDelimiter = $closeDelimiter; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the template file. Chris@0: * Chris@0: * @param string $file Chris@0: * @throws InvalidArgumentException Chris@0: */ Chris@0: public function setFile($file) Chris@0: { Chris@0: $distFile = $file . '.dist'; Chris@0: Chris@0: if (file_exists($file)) { Chris@0: $this->template = file_get_contents($file); Chris@0: } Chris@0: Chris@0: else if (file_exists($distFile)) { Chris@0: $this->template = file_get_contents($distFile); Chris@0: } Chris@0: Chris@0: else { Chris@0: throw new InvalidArgumentException( Chris@0: 'Template file could not be loaded.' Chris@0: ); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets one or more template variables. Chris@0: * Chris@0: * @param array $values Chris@0: * @param bool $merge Chris@0: */ Chris@0: public function setVar(array $values, $merge = TRUE) Chris@0: { Chris@0: if (!$merge || empty($this->values)) { Chris@0: $this->values = $values; Chris@0: } else { Chris@0: $this->values = array_merge($this->values, $values); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Renders the template and returns the result. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function render() Chris@0: { Chris@0: $keys = array(); Chris@0: Chris@0: foreach ($this->values as $key => $value) { Chris@0: $keys[] = $this->openDelimiter . $key . $this->closeDelimiter; Chris@0: } Chris@0: Chris@0: return str_replace($keys, $this->values, $this->template); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Renders the template and writes the result to a file. Chris@0: * Chris@0: * @param string $target Chris@0: */ Chris@0: public function renderTo($target) Chris@0: { Chris@0: $fp = @fopen($target, 'wt'); Chris@0: Chris@0: if ($fp) { Chris@0: fwrite($fp, $this->render()); Chris@0: fclose($fp); Chris@0: } else { Chris@0: $error = error_get_last(); Chris@0: Chris@0: throw new RuntimeException( Chris@0: sprintf( Chris@0: 'Could not write to %s: %s', Chris@0: $target, Chris@0: substr( Chris@0: $error['message'], Chris@0: strpos($error['message'], ':') + 2 Chris@0: ) Chris@0: ) Chris@0: ); Chris@0: } Chris@0: } Chris@0: } Chris@0: