Created
February 5, 2014 19:56
-
-
Save PabloVallejo/8831747 to your computer and use it in GitHub Desktop.
Mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ) ); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment