Skip to content

Instantly share code, notes, and snippets.

@alexsasharegan
Created August 15, 2016 17:35
Show Gist options
  • Save alexsasharegan/3d1ba075675ed9c507229ab82036b862 to your computer and use it in GitHub Desktop.
Save alexsasharegan/3d1ba075675ed9c507229ab82036b862 to your computer and use it in GitHub Desktop.
Use handlebars-like syntax and do template rendering with associative arrays, where the key is var name, and the value is the rendered content.
<?php
function assoc_replace ($templateStr, $replacementList) {
$searchStrings = [];
$replacements = [];
foreach ($replacementList as $key => $val) {
array_push($searchStrings, '{{'.$key.'}}');
array_push($replacements, $val);
}
return str_replace($searchStrings, $replacements, $templateStr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment