2017 © Pedro Peláez
 

library tower

A light weight helper for simple php templating

image

swaroopsm/tower

A light weight helper for simple php templating

  • Saturday, October 11, 2014
  • by swaroopsm
  • Repository
  • 0 Watchers
  • 4 Stars
  • 11 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Tower

Build Status Code Climate Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

A light weight helper for simple php templating., (*2)

Why use Tower?

If you want to keep your view logic separated from the core code, Tower is for you. Tower is obviously not a full-fledged template engine. Although if you are using a PHP framework like Codeigniter, Laravel etc., Tower is again a bad solution., (*3)

Tower is best suited if for small scale web applications, where you may not need a full-fledged framework. If you decide to write a web application using plain PHP, then do check Tower., (*4)

Installing Tower

Tower is available via composer. Add the following line to your composer.json so that Tower is autoloaded into your application., (*5)

"require": {
  "swaroopsm/tower": "0.3"
}

Using Tower

Tower has a very simple and easy API consisting of a few methods., (*6)

Instantiate Tower

$tower = new Tower();

Set a template file

Tell Tower which file should be used as the template., (*7)

// filename: template.php
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <h2><?= $name ?></h2>
  <p><?= $description ?></p>
</body>
</html>
$tower->setTemplate('template.php');

Set variables

Tower lets you setup variables that can be used in your template., (*8)

$tower->set('name', 'Tower');
$tower->set('description', 'A simple template helper');

Render your template

Render on your browser., (*9)

$tower->render();

Template Layouting

Tower also lets you add a layout for your templates. This can be useful if for your webpages. Example on using layout in your templates;, (*10)

$tower->setLayout('layout.php');

You use the $yield to render the template contents in your helper. A more detailed example on using layout is availabe at: Layout Example, (*11)

Using Partials

Partials allows you to include templates in other templates thus allowing you to re-use the templates. Refer to the following code in order to set partials for your templates., (*12)

$tower->partial->set('header', 'header.php');
$tower->partial->set('footer', 'footer.php');

And to render these partials in your templates use:, (*13)

<?= $partial['header'] ?>

Some stuffs here...

<?= $partial['footer'] ?>

If you decide to use a different variable for the partial instead of $partial use the following:, (*14)

$tower->partial->setPrefix('towerPartial');

Now you can do the following:, (*15)

<?= $towerPartial['header'] ?>

Some stuffs here...

<?= $towerPartial['footer'] ?>

Refer here for a Detailed Example, (*16)

Some Goodies

Few other extra methods included in Tower., (*17)

Save to file

This is useful if you would like to dynamically save contents to a file., (*18)

$tower->save('filename.txt');

Examples

The Versions

11/10 2014

dev-master

9999999-dev

A light weight helper for simple php templating

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Swaroop SM

templating template layout

11/10 2014

0.4

0.4.0.0

A light weight helper for simple php templating

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Swaroop SM

templating template layout

05/07 2014

0.3

0.3.0.0

A light weight helper for simple php templating

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Swaroop SM

templating template layout

29/06 2014

0.2

0.2.0.0

A light weight helper for simple php templating

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Swaroop SM

22/06 2014

0.1

0.1.0.0

A light weight helper for simple php templating

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Swaroop SM