PHP Classes

Mask: Trait to process templates with class functions

Recommend this page to a friend!
  Info   View files Example   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2022-10-21 (1 month ago) RSS 2.0 feedStarStarStarStar 69%Total: 181 This week: 1All time: 8,624 This week: 491Up
Version License PHP version Categories
mask 1.0.2MIT/X Consortium ...5PHP 5, Templates, Traits
Description Author

This class provides a trait to process templates with class functions.

It can be included on a class to process templates and use the class function call return values or variable values to replace in the template.

The template engine can compile the templates into PHP scripts and cache them to avoid further processing. It also supports:

1. Conditional sections
2. Foreach iteration loop sections
3. Include external templates

Innovation Award
PHP Programming Innovation award nominee
October 2016
Number 12
Template engines are frequently used in PHP applications to generate Web pages or some kind of other output from template files or strings, as they make it easy to separate login from presentation code.

Usually you use a component that processes a template file or a string and replaces some variables with values passed to the template engine.

This package provides an alternative approach by using a trait that can be used by any class. The template variables are replaced by the values of class variables or return values of class functions with the same names.

This way developers can easily turn templates into classes of their own that they can call to generate their applications output.

Manuel Lemos
Picture of Marco Afonso
Name: Marco Afonso <contact>
Classes: 1 package by
Country: Portugal Portugal
Innovation award
Innovation award
Nominee: 1x

Details

mask

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'
        );
    }
}
  Files folder image Files  
File Role Description
Files folder imagedemo (2 files, 2 directories)
Files folder imagesrc (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  demo  
File Role Description
Files folder imagecache (1 file)
Files folder imagetemplates (2 files)
  Plain text file DemoView.php Class Class source
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files  /  demo  /  cache  
File Role Description
  Accessible without login Plain text file mask_426fd72e0d52b...3fbcd5766dbf527.php Example Example script

  Files folder image Files  /  demo  /  templates  
File Role Description
  Accessible without login Plain text file layout.html Data Auxiliary data
  Accessible without login HTML file partial.html Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageTaviroquai (1 directory)

  Files folder image Files  /  src  /  Taviroquai  
File Role Description
Files folder imageMask (1 file)

  Files folder image Files  /  src  /  Taviroquai  /  Mask  
File Role Description
  Plain text file Mask.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:181
This week:1
All time:8,624
This week:491Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:83%StarStarStarStarStar
Documentation:75%StarStarStarStar
Examples:83%StarStarStarStarStar
Tests:-
Videos:-
Overall:69%StarStarStarStar
Rank:425