Skip to content

Instantly share code, notes, and snippets.

@fdorantesm
Created August 27, 2016 05:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdorantesm/e1967a17bfeb2fb258f245411cf9d439 to your computer and use it in GitHub Desktop.
Save fdorantesm/e1967a17bfeb2fb258f245411cf9d439 to your computer and use it in GitHub Desktop.
Simple parser
<?
function parse($view,$data){
$vars = array();
foreach($data as $key=>$value){
$vars[] = '{'.$key.'}';
}
return str_replace($vars, $data, $view);
}
function view($file,$data,$return=false){
$view = file_get_contents("./views/$file");
$html = parse($view,$data);
if($return)
return $html;
else
echo $html;
}
$data["title"] = "El blog de Pepito";
$data["nombre"] = "Pepito";
$data["edad"] = "25";
$data["main"] = view("main.php",$data,true);
view("site.php",$data);
<p>Soy {nombre} y tengo {edad} años</p>
<doctype html>
<html>
<head>
<title>{title}</title>
</head>
<body>
<header>
<div>Header</div>
<header>
<main class="container">
{main}
</main>
<footer>
<div>Footer</div>
</footer>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment