Skip to content

Instantly share code, notes, and snippets.

@assertchris
Forked from wayneashleyberry/t.js
Created September 28, 2012 13:19
Show Gist options
  • Save assertchris/3799805 to your computer and use it in GitHub Desktop.
Save assertchris/3799805 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}}!', {'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!';
*/
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