1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| <?php
function generate_template($template_path, $array)
{
if (file_exists($template_path))
$template = generate_match(file_get_contents($template_path), $array);
else
return false;
return $template;
}
function generate_match($template, $array)
{
preg_match_all('/<!-- IF (.*?) -->/U', $template, $match['if']);
preg_match_all('/<!-- LOOP (.*?) -->/U', $template, $match['loop']);
preg_match_all('/{(.*)}/U', $template, $match['string']);
$template = generate_template_if($template, $match['if'][1], $array);
$template = generate_template_loop($template, $match['loop'][1], $array);
$template = generate_template_string($template, $match['string'][1], $array);
return $template;
}
function generate_template_if($template, $match, $array)
{
for ($i = 0; $i < count($match); $i++)
{
if (!empty($array[$match[$i]]))
{
preg_match_all('/<!-- IF '.$match[$i].' -->(.*?)<!-- FI '.$match[$i].' -->/s', $template, $match_if);
for ($x = 0; $x < count($match_if[1]); $x++)
$template = preg_replace('/<!-- IF '.$match[$i].' -->(.*?)<!-- FI '.$match[$i].' -->/s', generate_match($match_if[1][$x], $array), $template);
$template = preg_replace('/<!-- ELSE '.$match[$i].' -->(.*?)<!-- ESLE '.$match[$i].' -->/s', '', $template);
}
else
{
preg_match_all('/<!-- ELSE '.$match[$i].' -->(.*?)<!-- ESLE '.$match[$i].' -->/s', $template, $match_if);
for ($x = 0; $x < count($match_if[1]); $x++)
$template = preg_replace('/<!-- ELSE '.$match[$i].' -->(.*?)<!-- ESLE '.$match[$i].' -->/s', generate_match($match_if[1][$x], $array), $template);
$template = preg_replace('/<!-- IF '.$match[$i].' -->(.*?)<!-- FI '.$match[$i].' -->/s', '', $template);
}
}
return $template;
}
function generate_template_loop($template, $match, $array)
{
for ($i = 0; $i < count($match); $i++)
{
if (!empty($array[$match[$i]]))
{
preg_match_all('/<!-- LOOP '.$match[$i].' -->(.*?)<!-- POOL '.$match[$i].' -->/s', $template, $match_loop);
for ($x = 0; $x < count($match_loop[1]); $x++)
$template = preg_replace('/<!-- LOOP '.$match[$i].' -->(.*?)<!-- POOL '.$match[$i].' -->/s', generate_match($match_loop[1][$x], $array[$match[$i]]), $template);
}
else
$template = preg_replace('/<!-- LOOP '.$match[$i].' -->(.*?)<!-- POOL '.$match[$i].' -->/s', '', $template);
}
return $template;
}
function generate_template_string($template, $match, $array)
{
for ($i = 0; $i < count($match); $i++)
{
if (!empty($array[$match[$i]]))
{
if (is_array($array[$match[$i]]))
{
for ($x = 0; $x < count($array[$match[$i]]); $x++)
{
if (empty($template_array[$x]))
$template_array[$x] = $template;
$template_array[$x] = str_replace('{'.$match[$i].'}', $array[$match[$i]][$x], $template_array[$x]);
}
}
else
$template = str_replace('{'.$match[$i].'}', $array[$match[$i]], $template);
}
else
$template = str_replace('{'.$match[$i].'}', '', $template);
}
if (!empty($template_array))
{
$template = '';
for ($i = 0; $i < count($template_array); $i++)
$template .= $template_array[$i];
}
return $template;
}
?> |