Last active
December 24, 2015 17:59
-
-
Save lzsucceed/6839518 to your computer and use it in GitHub Desktop.
This file contains 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 | |
if (! defined ( 'BASEPATH' )) | |
exit ( 'No direct script access allowed' ); | |
require_once APPPATH . '/third_party/src/Mustache/Autoloader.php'; | |
Mustache_Autoloader::register (); | |
/** | |
* helper library for Mustache. | |
* This library must be located in third_party folder. | |
* You can use like this: | |
* | |
* In controller: | |
* | |
* $data = array ( | |
* 'title' => 'bob's page' | |
* ); | |
* $this->mustache->render ('home', $data); // assumed you have home.php and home.html(template) | |
* | |
* In view: | |
* paste below in home.php | |
* <?php echo $M_PLATE | |
* | |
* Even if phpfile doesn't exist, it can find only template file and render it by echo(). | |
* | |
* @author lzy | |
* | |
*/ | |
class Mustache { | |
private $m; | |
private $CI; | |
// template's extention you use | |
private $EXTENSION = ".mustache"; | |
// it is used when you declare using template in php view file | |
// like <?php echo $M_PLATE | |
private $TEMPLATE = "M_PLATE"; | |
// resource path | |
private $RESOURCE_FOLDER; | |
// path where templatefiles are | |
private $VIEW_FOLDER; | |
/** | |
* Constractor can be autoloaded per request in application/config/config.php | |
*/ | |
function __construct() { | |
// Set folder path. | |
// Default resource folder is located in the folder | |
// where index.php is. | |
$this->RESOURCE_FOLDER = 'resources'; | |
$this->VIEW_FOLDER = APPPATH . 'views'; | |
$params = array ( | |
'loader' => new Mustache_Loader_FilesystemLoader ( $this->VIEW_FOLDER, array ( | |
'extension' => $this->EXTENSION | |
) ) | |
); | |
$this->m = new Mustache_Engine ( $params ); | |
$this->CI = get_instance (); | |
} | |
/** | |
* You can get instance for using manually ,but | |
* you can also call new Mustache_Engine instance directlly from anywhere. | |
*/ | |
function getInst() { | |
return $this->m; | |
} | |
/** | |
* Render template by view's filename. | |
* Name must be same between phpfile and templatefile. | |
* | |
* Even if phpfile doesn't exist, it can find only template file and render it by echo(). | |
* | |
* @param unknown $viewName | |
* phpfile and templatefile's name in view folder | |
* @param unknown $templateData | |
* @param string $options | |
* it is second parameter of load->view method used in controller | |
*/ | |
function render($viewName, $templateData, $options = NULL) { | |
$data = array (); | |
if (isset ( $options )) { | |
$data = $options; | |
} | |
$templateData ['resource'] = $this->RESOURCE_FOLDER; | |
$data [$this->TEMPLATE] = $this->m->render ( $viewName, $templateData ); | |
$viewFilePath = $this->VIEW_FOLDER . '/' . $viewName . '.php'; | |
if (file_exists ( $viewFilePath )) { | |
$this->CI->load->view ( $viewName, $data ); | |
} else { | |
echo $data [$this->TEMPLATE]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment