Skip to content

Instantly share code, notes, and snippets.

@wayneashleyberry
Created September 28, 2012 12:46
Show Gist options
  • Save wayneashleyberry/3799602 to your computer and use it in GitHub Desktop.
Save wayneashleyberry/3799602 to your computer and use it in GitHub Desktop.
simple templating
/**
* simple mustache-like templater
* http://mir.aculo.us/2011/03/09/little-helpers-a-tweet-sized-javascript-templating-engine/
* -----------------------------------------------------------------------------------------
* t('Hello, {{planet}}!', array('planet' => 'World');
* returns 'Hello, World!';
*/
function t(s,d){
for(var p in d)
s=s.replace(new RegExp('{{'+p+'}}','g'), d[p]);
return s;
}
/**
* simple mustache-like templater
* http://mir.aculo.us/2011/03/09/little-helpers-a-tweet-sized-javascript-templating-engine/
* -----------------------------------------------------------------------------------------
* t('Hello, {{planet}}!', array('planet' => 'World');
* returns 'Hello, World!';
*/
<?php
function t($str, $data)
{
$data = (array) $data;
foreach($data as $key => $val)
$str = str_replace('{{' . $key . '}}', $val, $str);
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment