2017 © Pedro Peláez
 

library theme

Simple, sane templating and theming for PHP

image

seanmorris/theme

Simple, sane templating and theming for PHP

  • Sunday, July 2, 2017
  • by seanmorris
  • Repository
  • 1 Watchers
  • 0 Stars
  • 200 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 46 % Grown

The README.md

SeanMorris\Theme

Simple, sane templating and theming for PHP

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

The aim of the Theme library is to provide a start separation of presentation from logic. It has only two responsibilities:, (*2)

  • Mapping objects to Views
  • Rendering those Views

Composer

Just run composer require seanmorris/theme in your project directory., (*3)

You can also add "seanmorris/theme": "^1.0.0" to the require array in your project's composer.json., (*4)

"require": {
  "seanmorris/theme": "^1.0.0"
}

Templating

Coupling the template with the View class is very simple. Simply subclass the provided View and append the template after a call to __halt_compiler(); (WITH THE CLOSING ?>) like so:, (*5)

(note: Short tags are enabled for simple echo statements as of PHP 5.4, but are not required), (*6)

<?php
class FooView extends \SeanMorris\Theme\View
{
}
__halt_compiler(); ?>
<h1>FooView</h1>
<span class = "some_class"><?=$a;?></span>
<p><?=$b;?>. <b><?=$c;?></b></p>

Pass an associative array into the constructor to populate the variables in the template. The keys of the array will be translated to variable names., (*7)

 'value'
  , 'b' => 'value'
  , 'c' => 'value'
]);

echo $view;
```

## Preprocessing

Preprocessing templates is simple. Just implement the ```preprocess``` method in your view class, and you'll get a chance to operate on the variables prior to rendering.

```php



FooView

<span class = "some_class">=$a;?></span>

=$b;?>. =$c;?>, (*8)

Usage:, (*9)

 'value'
  , 'b' => 'value'
  , 'c' => 'value'
]);

echo $view;
```

## Theming

Creating a theme is as simple as extending the theme class and providing a mapping from your object classes to their view classes, as shown:

```php
 'SeanMorris\Theme\FooView'
  ];
}
```

Usage:

```php

Wrapping

If you've got a default "trim" you'd like to use to wrap everything (i.e. the view that contains your , (*10)

, and structure), simply set the static property $wrap to an array listing your wrappers, innermost to outtermost.
<?php
class Theme extends \SeanMorris\Theme\Theme
{
  protected static
  $wrap = [
    'SeanMorris\Theme\Wrapper'
    , 'SeanMorris\Theme\HtmlDocument' 
  ];
}

Usage:, (*11)

<?php
$bodyText = 'Lorem ipsum dolor sit amet...';
echo Theme::wrap($bodyText);

Advanced Stuff...

Although the library doesn't do much, its got some power under the hood., (*12)

Fallback Themes

If a theme cannot render an object, it can defer the rendering to other themes that can. This is done by specifying the $themes static property. The list will be check in order, until a theme is able to render a given object., (*13)

<?php
class Theme extends \SeanMorris\Theme\Theme
{
  protected static
  $themes = [
    'SeanMorris\SomeTheme\Theme'
    , 'SeanMorris\SomeOtherTheme\Theme'
  ];
}

Subclassing Views

You can subclass any view class and keep the template by ommitting the call to __halt_compiler(), and extending the existing view. You'll probably want to override the parent preprocessor as well., (*14)

<?php
class FoozleView extends FooView
{
  public function preprocess(&$vars)
  {
    parent::preprocess($vars);
    $vars['a'] = $vars['object']->a . 'DIFFERENT!!!';
  }
}

Contextualized Themeing

By defining mappings from classes to views in the $contextViews array, you can specify that a view should be rendered differently when the render call is made from certain classes., (*15)

In this example SeanMorris\Stuff\Foo will be rendered with the SeanMorris\Theme\FooAlternateView class when rendered inside of the SeanMorris\Stuff\RandomObject class, but outside, it will be rendered with SeanMorris\Theme\FooView., (*16)

<?php
class Theme extends \SeanMorris\Theme\Theme
{
  protected static
  $contextView = [
    'SeanMorris\Stuff\RandomObject' => [
      'SeanMorris\Stuff\Foo' => 'SeanMorris\Theme\FooAlternateView'
    ]
  ]
  , $view = [
    'SeanMorris\Stuff\Foo' => 'SeanMorris\Theme\FooView'
  ];
}

The Versions

02/07 2017

dev-master

9999999-dev

Simple, sane templating and theming for PHP

  Sources   Download

Apache-2.0

The Development Requires

by Sean Morris

02/07 2017

1.0.7

1.0.7.0

Simple, sane templating and theming for PHP

  Sources   Download

Apache-2.0

The Development Requires

by Sean Morris

28/02 2016

1.0.6

1.0.6.0

Simple, sane templating and theming for PHP

  Sources   Download

Apache-2.0

The Development Requires

by Sean Morris

28/02 2016

1.0.5

1.0.5.0

Simple, sane templating and theming for PHP

  Sources   Download

Apache-2.0

The Development Requires

by Sean Morris

28/02 2016

1.0.4

1.0.4.0

Simple, sane templating and theming for PHP

  Sources   Download

Apache-2.0

The Development Requires

by Sean Morris

22/02 2016

1.0.3

1.0.3.0

Simple, sane templating and theming for PHP

  Sources   Download

Apache-2.0

The Development Requires

by Sean Morris

20/02 2016

1.0.2

1.0.2.0

Simple, sane templating and theming for PHP

  Sources   Download

Apache-2.0

The Development Requires

by Sean Morris

15/02 2016

1.0.1

1.0.1.0

Simple, sane templating and theming for PHP

  Sources   Download

Apache-2.0

The Development Requires

by Sean Morris

14/02 2016

1.0.0

1.0.0.0

Simple, sane templating and theming for PHP

  Sources   Download

The Development Requires

by Sean Morris