Skip to content
This repository has been archived by the owner on Aug 6, 2020. It is now read-only.

Commit

Permalink
first commit. version 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Cordazar committed Sep 8, 2011
0 parents commit be03085
Show file tree
Hide file tree
Showing 7 changed files with 1,134 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/autoload.php
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');
159 changes: 159 additions & 0 deletions libraries/mustache_spark.php
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;
}

}
52 changes: 52 additions & 0 deletions readme.md
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).
3 changes: 3 additions & 0 deletions spark.info
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
Loading

0 comments on commit be03085

Please sign in to comment.