Archivo: index.tpl.html
Código HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Template</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> ¡Hola! ¿Qué tal estáis {a}, {b}, {c} y {d}? </body> </html>
Código PHP:
<?php
function template($ruta,$array)
{
/* Tomamos el archivo */
$template = file_get_contents("$ruta");
/* Reemplazamos el contenido entre corchetes por la etiqueta PHP */
foreach ($array as $a => $b) {
$template = str_replace("{".$a."}", "$b", $template);
}
/* Mostramos el template con los cambios realizados */
echo eval("?".">$template");
}
/* Variable al azar */
$rand = "1";
/* Establecemos las variables */
$ruta = "./index.tpl.html";
$array = array(
"a" => "<?php echo $rand; ?>",
"b" => "<?php echo 'Pepe'; ?>",
"c" => "<?php echo 'Manolito'; ?>",
"d" => "<?php echo 'Pedro'; ?>"
);
/* Llamamos a la función */
template($ruta,$array);
?>
![sonriente](http://static.forosdelweb.com/fdwtheme/images/smilies/smile.png)