├── .gitignore ├── composer.json ├── README.markdown └── View └── Mustache.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hashchange/slim-legacy-mustache", 3 | "type": "library", 4 | "description": "Mustache support for Slim 1.x, backward compatible with PHP 5.2", 5 | "keywords": ["templating","extensions","mustache","slim","legacy"], 6 | "homepage": "http://github.com/hashchange/Slim-Legacy-Mustache", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Johnson Page", 11 | "homepage": "http://johnsonpage.org/" 12 | }, 13 | { 14 | "name": "Justin Hileman", 15 | "homepage": "http://justinhileman.info/" 16 | }, 17 | { 18 | "name": "Michael Heim", 19 | "homepage": "http://www.zeilenwechsel.de/" 20 | } 21 | ], 22 | "require": { 23 | "php": ">=5.2.0", 24 | "slim/slim": "1.*", 25 | "mustache/mustache": "2.*" 26 | }, 27 | "autoload": { 28 | "psr-0": { 29 | "": "" 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # Mustache support for Slim, backward compatible with PHP 5.2 2 | 3 | [Slim](http://www.github.com/codeguy/Slim) is a micro PHP 5 framework that helps you quickly write simple yet powerful RESTful web applications. Its [1.x branch](http://github.com/codeguy/Slim/tree/1.6.7) is backward compatible with PHP 5.2. 4 | 5 | This repository contains a custom Mustache view for Slim 1.x, offering full support for [Mustache.php](http://github.com/bobthecow/mustache.php). 6 | 7 | ## Where it differs from the "official" repo 8 | 9 | Mustache support for Slim is provided by a single class. It is based on the [Mustache view](https://github.com/codeguy/Slim-Extras/blob/develop/Views/Mustache.php) contained in [Slim-Extras](http://github.com/codeguy/Slim-Extras), but is customized in a number of ways. 10 | 11 | Functional differences: 12 | 13 | - This version of the class is compatible with PHP 5.2 and does not use namespaces. 14 | - The class allows an object to be used as view data, thus enabling the use of [Mustache lambdas](http://mustache.github.com/mustache.5.html) in PHP 5.2. The version in the official repo only supports arrays. 15 | 16 | Implementation details: 17 | 18 | - The class requires Slim 1.x and won't work with Slim 2.x (use Slim-Extras for that). It extends `Slim_View`, not `\Slim\View`. 19 | - The class is called `View_Mustache`, not `Mustache`. It has been renamed to avoid conflicts in the absence of namespaces. 20 | 21 | Otherwise, it is used in the exact same way as the class provided with [Slim-Extras](http://github.com/codeguy/Slim-Extras). See there for usage notes. 22 | 23 | ## Installation 24 | 25 | Slim-Legacy-Mustache is best installed with Composer and is available from Packagist as "hashchange/slim-legacy-mustache". 26 | 27 | The following setup in `composer.json` will install Slim and Mustache for PHP 5.2 in one go: 28 | 29 | { 30 | "require": { 31 | "slim/slim": "1.*", 32 | "hashchange/slim-legacy-mustache": "*", 33 | "mustache/mustache": "2.*" 34 | } 35 | } 36 | 37 | ## Version 38 | 39 | The current version is based on [Mustache.php @ b12fdd0](https://github.com/codeguy/Slim-Extras/blob/b12fdd069062a0d30d1584aad3aa5bd76c275c5e/Views/Mustache.php) in Slim-Extras 2.0.3-develop, last updated on [19 Jan 2013](https://github.com/codeguy/Slim-Extras/commits/develop/Views/Mustache.php). It was tested with [Slim 1.6.7](http://github.com/codeguy/Slim/tree/1.6.7). 40 | 41 | ## Related 42 | 43 | **Slim** 44 | 45 | [Primary Slim repo](http://www.github.com/codeguy/Slim) 46 | [Primary Slim-Extras repo](http://github.com/codeguy/Slim-Extras) 47 | [Slim website](http://www.slimframework.com/) 48 | 49 | **Mustache** 50 | 51 | [Primary Mustache.php repo](http://github.com/bobthecow/mustache.php) 52 | [Mustache.php wiki](http://github.com/bobthecow/mustache.php/wiki) 53 | [Mustache website](http://mustache.github.com/) 54 | 55 | ## Open Source License 56 | 57 | The resources in this repository are released under the MIT public license. 58 | 59 | 60 | -------------------------------------------------------------------------------- /View/Mustache.php: -------------------------------------------------------------------------------- 1 | 60 | * @author Justin Hileman 61 | * @author Michael Heim 62 | */ 63 | class View_Mustache extends Slim_View 64 | { 65 | /** 66 | * @var string The path to the directory containing Mustache.php. 67 | * Not required if an autoloader covering Mustache.php is 68 | * already in place. 69 | */ 70 | public static $mustacheDirectory = null; 71 | 72 | /** 73 | * @var array An array of Mustache_Engine options 74 | */ 75 | public static $mustacheOptions = array(); 76 | 77 | /** 78 | * @var Mustache_Engine A Mustache engine instance for this view 79 | */ 80 | private $engine = null; 81 | 82 | /** 83 | * @param array $options Mustache_Engine configuration options 84 | */ 85 | public function __construct(array $options = null) 86 | { 87 | if ($options !== null) { 88 | self::$mustacheOptions = $options; 89 | } 90 | } 91 | 92 | /** 93 | * Append data to existing View data. Accepts arrays as well as an object 94 | * (for PHP-5.2-compatible lambda functions in Mustache). 95 | * 96 | * Note that you can append arrays to an existing object, or an object to an 97 | * existing array, but NOT an object to an object. Use one object only, at 98 | * the most. 99 | * 100 | * @throws InvalidArgumentException 101 | * @param array|Object $data 102 | * @return void 103 | */ 104 | public function appendData( $data ) 105 | { 106 | if ( is_object( $this->data ) and is_object($data) ) 107 | { 108 | // Can't merge two objects safely, internal state might be lost 109 | throw new InvalidArgumentException("Can't merge view data of multiple objects"); 110 | } 111 | elseif ( is_object( $this->data ) ) 112 | { 113 | foreach ( $data as $property => $item ) $this->data->$property = $item; 114 | } 115 | elseif ( is_object( $data ) ) 116 | { 117 | foreach ( $this->data as $property => $item ) $data->$property = $item; 118 | $this->data = $data; 119 | } 120 | else 121 | { 122 | $this->data = array_merge( $this->data, $data ); 123 | } 124 | } 125 | 126 | /** 127 | * Renders a template using Mustache.php. 128 | * 129 | * @see View::render() 130 | * @param string $template The template name specified in Slim::render() 131 | * @return string 132 | */ 133 | public function render($template) 134 | { 135 | return $this->getEngine()->render($template, $this->data); 136 | } 137 | 138 | /** 139 | * Get a Mustache_Engine instance. 140 | * 141 | * @return Mustache_Engine 142 | */ 143 | private function getEngine() 144 | { 145 | if (!isset($this->engine)) { 146 | // Check for Composer autoloading 147 | if (!class_exists('Mustache_Engine')) { 148 | require_once self::$mustacheDirectory . '/Autoloader.php'; 149 | Mustache_Autoloader::register(dirname(self::$mustacheDirectory)); 150 | } 151 | 152 | $options = self::$mustacheOptions; 153 | 154 | // Autoload templates from the templates directory. 155 | if (!isset($options['loader'])) { 156 | $options['loader'] = new Mustache_Loader_FilesystemLoader($this->getTemplatesDirectory()); 157 | } 158 | 159 | // If a partials loader is not specified, fall back to the default template loader. 160 | if (!isset($options['partials_loader']) && !isset($options['partials'])) { 161 | $options['partials_loader'] = $options['loader']; 162 | } 163 | 164 | $this->engine = new Mustache_Engine($options); 165 | } 166 | 167 | return $this->engine; 168 | } 169 | } 170 | --------------------------------------------------------------------------------