Skip to content

Templates

Jaime A. Rodriguez edited this page Sep 29, 2020 · 11 revisions

Concepts

Each page in SleepyMUSTACHE consists of two main elements: Templates and Placeholders. The Template is an HTML page that can contain variables which we call placeholders. Placeholders are useful to make templates reusable. On a basic page template, we may want to have a placeholder for the heading and the body copy.


<!-- simple.tpl -->
<html>
  <body>
    <h1>{{ heading }}</h1>
    <main>
      {{ body }}
    </main>
  </body>
</html>

In this example file, simple.tpl, you can see the placeholders are surrounded by {{ and }}.


We can use the template to build a simple webpage by binding data to the placeholders.

require_once $_SERVER['DOCUMENT_ROOT'] . '/app/sleepy/bootstrap.php';

use \Sleepy\Core\Template;

$page = new Template('simple');
$page->bind('heading', 'My Webpage');
$page->bind('body', 'sleepyMUSTACHE is so easy to use!');

After some bootstrapping boilerplate code, we create a new Template object and pass in the templates name: simple. We then call the Template::bind() method to replace the placeholders with our chosen text.

Branching Logic

Looping

Including Other Templates

Best Practices

Clone this wiki locally