Skip to content

Instantly share code, notes, and snippets.

@ichiriac
Forked from nrutman/renderTemplate.php
Last active December 10, 2015 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ichiriac/4427022 to your computer and use it in GitHub Desktop.
Save ichiriac/4427022 to your computer and use it in GitHub Desktop.
<?php
function renderTemplate($template, $data) {
$data_keys = array_map(function($key) {
return '%' . $key . '%';
}, array_keys($data));
$data_values = array_values($data);
return str_replace($data_keys, $data_values, $template);
}
/*
USAGE:
$template = '<%tag% %attr% class="poster pos-%pos% %type%"><img src="%img_url%" alt="%desc%"/><span class="screen"></span></%tag%>';
foreach($posters as $i => $poster) {
$t_data = array(
'tag' => ($poster->link) ? 'a' : 'span',
'attr' => ($poster->link) ? 'href="' . $poster->link . '" target="_blank"' : '',
'pos' => $i,
'type' => ($i == 1) ? 'featured' : '',
'img_url' => ($i == 1) ? $poster->fields['image']['sizes']['poster'] : $poster->fields['image']['sizes']['poster-small'],
'desc' => $poster->description
);
echo renderTemplate($template, $t_data);
endforeach
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment