This repository has been archived by the owner on Aug 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit be03085
Showing
7 changed files
with
1,134 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | ||
|
||
$autoload['libraries'] = array('Mustache_spark'); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); | ||
|
||
/* | ||
require_once '../vendor/Mustache.php'; | ||
*/ | ||
|
||
/** | ||
* Implementation of the Mustache template system for CodeIgniter | ||
*/ | ||
class Mustache_spark | ||
{ | ||
|
||
private $master_template; | ||
private $template_collection; | ||
private $data_collection; | ||
private $template_file_extension; | ||
private $spark_path; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @access public | ||
* @return void | ||
*/ | ||
public function __construct() { | ||
$this->spark_path = dirname(__FILE__) . '/'; | ||
// Setting standard file extension to 'tpl' to reinforce | ||
// that mustache templates doesn't need php code in them | ||
$this->template_file_extension = 'tpl'; | ||
$this->data_collection = array(); | ||
$this->template_collection = array(); | ||
$this->CI =& get_instance(); | ||
log_message('debug', "Mustache Class Initialized"); | ||
} | ||
|
||
/** | ||
* Get template file if it exists. | ||
* | ||
* @access private | ||
* @param string $template | ||
*/ | ||
private function get_include_contents($template) | ||
{ | ||
// Build file path based on CI's structure | ||
$file_path = APPPATH.'views/'.$template.'.tpl'; | ||
if ( is_file($file_path) ) | ||
{ | ||
ob_start(); | ||
include($file_path); | ||
$contents = ob_get_contents(); | ||
ob_end_clean(); | ||
return $contents; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Render everything to CodeIgniters output, once is enough | ||
* | ||
* @access public | ||
* @return void | ||
*/ | ||
public function render() | ||
{ | ||
$vendor_library_path = $this->spark_path . '../vendor/Mustache.php'; | ||
if(file_exists($vendor_library_path)) | ||
{ | ||
require_once($vendor_library_path); | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
$mustache = new Mustache; | ||
$this->CI->output->append_output( | ||
$mustache->render( | ||
$this->master_template, | ||
$this->data_collection, | ||
$this->template_collection | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Get data | ||
* | ||
* @access public | ||
* @return array | ||
*/ | ||
public function get_data() | ||
{ | ||
return $this->data_collection; | ||
} | ||
|
||
/** | ||
* Merge data into total data array | ||
* | ||
* @access public | ||
* @param array $module_data | ||
* @return void | ||
*/ | ||
public function merge_data($module_data) | ||
{ | ||
$this->data_collection = array_merge_recursive( | ||
$this->data_collection, | ||
$module_data | ||
); | ||
} | ||
|
||
/** | ||
* Set master template | ||
* | ||
* @access public | ||
* @param string $template | ||
* @return void | ||
*/ | ||
public function set_master_template($template) | ||
{ | ||
$this->master_template = self::get_include_contents($template); | ||
} | ||
|
||
/** | ||
* Merge templates into the total template array | ||
* | ||
* @access public | ||
* @param array $mustache_templates | ||
* @return boolean | ||
*/ | ||
public function merge_template($mustache_templates) | ||
{ | ||
foreach($mustache_templates as $partial => $template_file_name) | ||
{ | ||
$template = self::get_include_contents($template_file_name); | ||
if ( $template ) | ||
{ | ||
if ( isset($this->template_collection[$partial]) ) | ||
{ | ||
$this->template_collection[$partial] .= $template; | ||
} | ||
else | ||
{ | ||
$this->template_collection[$partial] = $template; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Set template file extension | ||
* | ||
*/ | ||
public function set_template_file_extension($file_extension) { | ||
$this->template_file_extension = $file_extension; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
Mustache-Spark | ||
============== | ||
|
||
Mustache-Spark implements support for using the Mustache template system in CodeIgniter. | ||
|
||
Introduction | ||
------------ | ||
|
||
Mustache is available for Codeigniter via [Sparks](http://getsparks.org/install). | ||
|
||
Once you've got the spark set up, you can load it using: | ||
|
||
$this->load->spark('mustache_spark/[version #]'); | ||
|
||
When Mustache-Spark is loaded, we can get on to more exciting things. | ||
|
||
### Set master template | ||
|
||
$this->mustache_spark->set_master_template( | ||
'comment' | ||
); | ||
|
||
### Adding data to mustache | ||
|
||
$this->mustache_spark->merge_data( | ||
array( | ||
'comment' => 'A comment text thingy.' | ||
) | ||
); | ||
|
||
### Merge template(s) into mustache | ||
|
||
$this->mustache_spark->merge_template( | ||
array( | ||
'header' => 'header', | ||
'footer' => 'footer' | ||
) | ||
); | ||
|
||
### Render all when you are done | ||
|
||
$this->mustache_spark->render(); | ||
|
||
Author | ||
------ | ||
|
||
Ricard Aspeljung <cordazar@gmail.com> | ||
|
||
License | ||
------- | ||
|
||
Mustache-Spark is released under the DBAD License. You can read the license [here](http://philsturgeon.co.uk/code/dbad-license). |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
name: mustache_spark | ||
version: 0.0.1 | ||
compatibility: 2.0.3 |
Oops, something went wrong.