Skip to content
Swaroop SM edited this page Jul 5, 2014 · 1 revision

Using partials for your templates

This is an example of using partials in your template.

Create 5 files:

  • layout.php
  • template.php
  • sample.php
  • header.php
  • footer.php

Open layout.php and paste the following code:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Tower</title>
</head>
<body>
<?= $partial['header'] ?>
<?= $yield ?>
</body>
</html>

Open template.php and paste the following code:

<h1><?= $name ?></h1>
<p><?= $age ?></p>
<?= $partial['footer'] ?>

Note the use of $partial in layout.php and template.php to demonstrate the power of partials.

Open sample.php and paste the following code:

<?php

require 'vendor/autoload.php';

$tower = new Tower();
$tower->setLayout('layout.php');
$tower->setTemplate('template.php');
$tower->partial->set('header', 'header.php');
$tower->partial->set('footer', 'footer.php');
$tower->set('name', 'Swaroop SM');
$tower->set('age', 24);
$tower->set('profession', 'Software Developer');
$tower->render();

Open header.php and paste the following code:

<h1>Header</h1>

Open footer.php and paste the following code:

<strong>footer</strong>

Open sample.php on your browser.

--gg

Clone this wiki locally