PHP Classes
elePHPant
Icontem

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
2016-09-13 (3 years ago) RSS 2.0 feedStarStarStarStar 69%Total: 176 All time: 8,475 This week: 309Up
Version License PHP version Categories
mask 1.0.0MIT/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
Name: Marco Afonso <contact>
Classes: 1 package by
Country: Portugal Portugal
Age: ???
All time rank: 406245 in Portugal Portugal
Week rank: 747 Up13 in Portugal Portugal Up
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:176
This week:0
All time:8,475
This week:309Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:83%StarStarStarStarStar
Documentation:75%StarStarStarStar
Examples:83%StarStarStarStarStar
Tests:-
Videos:-
Overall:69%StarStarStarStar
Rank:419