PHP Classes

Mask: Trait to process templates with class functions

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 69%Total: 183 All time: 8,683 This week: 455Up
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
Age: ???
All time rank: 419947 in Portugal Portugal
Week rank: 178 Up3 in Portugal Portugal Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php

error_reporting
(E_ALL);
ini_set('display_errors', 1);

// Load dependencies
require_once '../vendor/autoload.php';

// Tell what we will use
use Taviroquai\Mask\Mask;

// Set configuration
Mask::$templateCachePath = './cache';
Mask::$templatePath = './templates';

// Load demo
require_once 'DemoView.php';

// Run demo
$view = new LayoutView();
echo
$view->mask('layout');


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 (10)  
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 (10)  /  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 (10)  /  demo  /  cache  
File Role Description
  Accessible without login Plain text file mask_426fd72e0d52b...3fbcd5766dbf527.php Example Example script

  Files folder image Files (10)  /  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 (10)  /  src  
File Role Description
Files folder imageTaviroquai (1 directory)

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

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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:183
This week:0
All time:8,683
This week:455Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:83%StarStarStarStarStar
Documentation:75%StarStarStarStar
Examples:83%StarStarStarStarStar
Tests:-
Videos:-
Overall:69%StarStarStarStar
Rank:304