Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Template Example
<?php
$template = "I am {{name}}, and I work for {{company}}. I am {{age}}.";
# Your template tags + replacements
$replacements = array(
'name' => 'Jeffrey',
'company' => 'Envato',
'age' => 27
);
function bind_to_template($replacements, $template) {
return preg_replace_callback('/{{(.+?)}}/', function($matches) use ($replacements) {
return $replacements[$matches[1]];
}, $template);
}
// I am Jeffrey, and I work for Envato. I am 27.
echo bind_to_template($replacements, $template);
@silv3rm00n

This comment has been minimized.

Copy link

silv3rm00n commented Nov 22, 2012

'Mike', 'amount' => 400, ); function fill_template($template, $replacements) { $final = preg_replace('#{{([^{}]+)}}#sie' , "\$replacements['\${1}']" , $template); ``` return $final; ``` } echo fill_template($template, $row);
@silv3rm00n

This comment has been minimized.

Copy link

silv3rm00n commented Nov 22, 2012

'Mike', 'amount' => 400, ); function fill_template($template, $replacements) { $final = preg_replace('#{{([^{}]+)}}#sie' , "\$replacements['\${1}']" , $template); ``` return $final; ``` } echo fill_template($template, $row);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.