sbstjn/sliphp

Atomic view and layout engine for PHP 5.4 and above

0.2.1 2015-10-17 19:37 UTC

This package is not auto-updated.

Last update: 2024-11-09 18:32:55 UTC


README

Circle CI Travis CI Scrutinizer
Scrutinizer Code Quality Scrutinizer Code Coverage
Packagist MIT License

Atomic view and layout engine for PHP 5.4 and above.

Usage

To use SliPHP please configure the global constant SLIPHP_VIEWS and set it to the directory where your views, layouts and blocks are located.

Installation

composer require sbstjn/sliphp

Basics

As mentioned above, just configure the needed constant and you are good to go …

define ('SLIPHP_VIEWS', '/var/www/views/');

The folder structure inside your SLIPHP_VIEWS path should look something like this:

/var/www/views/index.php (View)
/var/www/views/layouts/default.php (Layout)
/var/www/views/blocks/header.php (Block)

Views

It's that simple to render a view from your SLIPHP_VIEWS folder:

$view = new SliPHP\View('index');
die($view);

Your index.php could look like this:

<strong>Hi!</strong>

Layouts

Of course you want to have a separate file for your layout, which will be wrapped around your index view:

$layout = new SliPHP\Layout('default');
$layout->body(new SliPHP\View('index'));

die($layout);

Your layouts/default.ph file could look like this:

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <title>SliPHP</title>
    <meta charset="utf-8" />
  </head>
  <body><?=$view->body()?></body>
</html>

As you might guess, the content of your view gehts inserted inside the body tag. Woho, Magic …

Blocks

For sure SliPHP supports loading single files and have them rendered into your views and layouts. We call them blocks, just use the ->block() method for loading them:

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <title>SliPHP Example</title>
    <?=$view->block('header')?>
  </head>
  <body><?=$view->body()?></body>
</html>

Your blocks/header.php could look like this:

<meta charset="utf-8" />

Blocks can load other blocks as well, so feel free to move your styles and script to separate files for example:

<meta charset="utf-8" />

<?=$view->block('styles')?>
<?=$view->block('script')?>

Apply

If you want to apply a function or a list of functions to a data object, you can use the $view->apply function to modify your data:

<strong><?=$view->apply('KATZE', 'ucfirst|strtolower')?><?=$view->apply('KATZE', 'strtolower|ucfirst')?></strong>

Will be rendered into:

<strong>katzeKatze</strong>

Helper

There is built in support for custom helper functions in SliPHP, just define your helpers like this:

$view = new SliPHP\View('index');

$view->helper('strong', function($value) {
    return '<strong>' . $value . '</strong>';
});

And call them inside your views with:

<?=$view->strong('hi')?>

You can for example use this to easily include your stylesheets:

$view->helper('css', function($file) {
    return '<link rel="stylesheet" href="' . $file . '" type="text/css" charset="utf-8" />';
});

Locals

Of course you can pass data to your views, SliPHP calls them Locals

$view = new SliPHP\View('index');
$view->set('foo', 'value');

You can access all your data using the $data variable inside your layouts, views and of course blocks:

<?=$data->foo?>

It's also possible to share locals between your layout and your views:

$layout = new SliPHP\Layout('default');
$layout->set('foo', 'value');
$layout->body('index');

Inside your index view you can now easily access to layout's locals:

<?=$data->foo?>

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email mail@sbstjn.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.