A PHP trait as a basic template engine

v1.0.0 2015-03-19 00:36 UTC

This package is auto-updated.

Last update: 2024-04-08 06:15:42 UTC


README

Mask is a PHP trait that functions as a basic PHP template engine

Tutorial

create a simple view to hold ALL view logic

class MyView
{
    protected $title = 'Hello';
    protected function logic()
    {
        return 'World!';
    }
}

add mask

use Taviroquai\Mask\Mask;

class MyView
{
    use Mask;
    
    protected $title = 'Hello';
    protected function logic()
    {
        return 'World!';
    }
}

now create an HTML file: template.html

<p>
{{ title }}
{{ if logic }}{{ logic }}{{ endif }}
</p>

finally use it in PHP as

$view = new MyView;
echo $view->mask('template');

output:

<p>
Hello
World!
</p>

API

Call variables and methods

{{ variableName }} {{ methodName }}

Conditions

{{ if methodOrVariableName }} ... something ... {{ endif }}

Foreach loops

{{ for variable as local }}
{{ local }}
{{ endfor }}

Includes

include partial.html
{{ include partial }}

Options

// Set cache path
Mask::$templateCachePath = './path/to/cache';

// Set templates path
Mask::$templatePath = './path/to/templates';

// Choose what properties to publish by overriding getMaskData()
class MyView
{
    use Mask;
    
    protected $property1;
    protected $property2;
    
    public function getMaskData()
    {
        return array(
            'property2' => 'override'
        );
    }
}