PabloVallejo
2/5/2014 - 7:56 PM

Mustache

Mustache

<?php
/**
*
* Mustache model
*/



/**
* Mustache model class
*/
class Mustache {

	/**
	 * Views folder
	 * @var str
	 */
	var $views_path;

	/**
	 * Mustache engine
	 * @var obj
	 */
	var $engine;


	/**
	 * Constructor
	 *
	 * @param { str } views folder
	 */
	public function __construct( $views_path = null ) {

		// Register autoload
		Mustache_Autoloader::register();

		// Default views folder
		$this->views_path = VIEWS_PATH . 'email';

		if ( is_string( $views_path ) )
			$this->views_path = $views_path;

		// Get mustache engine
		$this->engine = new Mustache_Engine(
			array(
				'loader' => new Mustache_Loader_FilesystemLoader( $this->views_path )
			)
		);
	}


	/**
	 * Return a new class instance
	 *
	 * @param { str } views folder
	 * @return { obj } class instance
	 */
	public static function make( $views_path = null ) {

		return new self( $views_path );
	}

	/**
	 * Render a template with the passed arguments.
	 *
	 * <code>
	 *
	 *		// Render an email template
	 *		$template = Mustache::make()->render( 'email', array( 'name' => 'John' ) );
	 *
	 * 		// Render a template in an secific folder
	 * 		$template = Mustache::make( 'c:/xampp/my-folder' )->render( 'template', array( 'age' => 20 ) );
	 *
	 * </code>
	 *
	 * @param { str } view filename without ".mustache"
	 * @param { arr } parameters to pass to the template
	 * @return { str } rendered template
	 */
	public function render( $file, $args = array() ) {

		$template = $this->engine->loadTemplate( $file );

		return html_entity_decode( $template->render( $args ) );
	}


}



?>