-
-
Save assertchris/3799805 to your computer and use it in GitHub Desktop.
simple templating
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
/** | |
* 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; | |
} |
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
/** | |
* 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