Hello! Greeting, Improved
Three files: one code; two templates
- index.php
- greeting.html
- form.html
index.php
<?php
$name = $_GET["name"];
// figure out which template is needed
if ($name) {
$template_file = 'greeting.html';
} else {
$template_file = 'form.html';
}
$template_contents = file_get_contents($template_file);
$template_data['name'] = $name;
// Process with Mustache Template
include('lib/Mustache.php');
$m = new Mustache;
echo $m->render($template_contents, $template_data);
?>
greeting.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Greeting Page for {{name}}</title>
</head>
<body>
<h1>Hello, {{name}}!</h1>
</body>
</html>
form.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Greeting Page</title>
</head>
<body>
<h1><label for="input_name">Enter name:</label></h1>
<form method="get">
<input type="text" name="name" id="input_name"/>
<br /><input type="submit" />
<br /><input type="reset" />
</body>
</html>