Created
August 27, 2016 05:51
-
-
Save fdorantesm/e1967a17bfeb2fb258f245411cf9d439 to your computer and use it in GitHub Desktop.
Simple parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p>Soy {nombre} y tengo {edad} años</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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