kostache/classes/Mustache/Loader/Kohana.php

63 lines
1.3 KiB
PHP
Raw Normal View History

2012-10-24 02:17:02 +03:00
<?php
2012-11-01 03:35:08 +02:00
class Mustache_Loader_Kohana implements Mustache_Loader, Mustache_Loader_MutableLoader
2012-10-24 02:17:02 +03:00
{
private $_base_dir = 'templates';
2012-10-24 06:22:15 +03:00
private $_extension = 'mustache';
2012-10-24 02:17:02 +03:00
private $_templates = array();
2012-10-24 06:22:15 +03:00
public function __construct($base_dir = NULL, $options = array())
2012-10-24 02:17:02 +03:00
{
2012-10-24 06:22:15 +03:00
if ($base_dir)
$this->_base_dir = $base_dir;
2012-10-24 02:17:02 +03:00
if (isset($options['extension']))
{
$this->_extension = '.' . ltrim($options['extension'], '.');
}
}
public function load($name)
{
if (!isset($this->_templates[$name]))
{
$this->_templates[$name] = $this->_load_file($name);
}
return $this->_templates[$name];
}
2012-10-24 06:22:15 +03:00
protected function _load_file($name)
2012-10-24 02:17:02 +03:00
{
$filename = Kohana::find_file($this->_base_dir, $name, $this->_extension);
if ( ! $filename)
{
throw new Kohana_Exception('Mustache template ":name" not found', array(':name' => $name));
}
2012-10-24 06:22:15 +03:00
return file_get_contents($filename);
2012-10-24 02:17:02 +03:00
}
2012-11-01 03:35:08 +02:00
/**
* Set an associative array of Template sources for this loader.
*
* @param array $templates
*/
public function setTemplates(array $templates)
{
$this->_templates = array_merge($this->_templates, $templates);
}
/**
* Set a Template source by name.
*
* @param string $name
* @param string $template Mustache Template source
*/
public function setTemplate($name, $template)
{
$this->_templates[$name] = $template;
}
2012-10-24 02:17:02 +03:00
}