PHP Classes

Absint PHP Template Engine Trait: Trait to render templates from inside any class

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
Not yet rated by the usersTotal: 76 All time: 10,170 This week: 47Up
Version License PHP version Categories
absint 1.1Freeware7Templates, PHP 7, Traits
Description 

Author

This package provides a trait to render templates from inside any class.

It implements a template engine in a trait that can parse a template given string and replace template marks by given variables values or values of variables of the class.

The trait can accept a given file as a template and bind a custom function to the replacement process.

Picture of wim niemans
  Performance   Level  
Name: wim niemans <contact>
Classes: 6 packages by
Country: The Netherlands The Netherlands
Age: ???
All time rank: 345989 in The Netherlands The Netherlands
Week rank: 180 Up6 in The Netherlands The Netherlands Up
Innovation award
Innovation award
Nominee: 2x

Example

<?php

include_once 'absint.trait.php';

Class
Person
{
use
absint;

public
$firstName = '';
public
$lastName = '';

}

$p = new Person;
// example with whitespace in variable names
$v = ['firstName' => 'John',
     
'middleName' => 'Fitzgerald',
     
'lastName' => 'Kennedy',
     ];
echo
$p->parse('Hello from { firstName } { middleName} {lastName }!', $v);
echo
" \n";
// example with empty variable
$v = ['firstName' => 'Robert',
     
'middleName' => '',
     
'lastName' => 'Kennedy',
     ];
echo
$p->parse('Hello from {firstName} {middleName} {lastName}!', $v);
echo
" \n";
// example with missing variable
$v = ['firstName' => 'George',
     
'lastName' => 'Washington',
     ];
echo
$p->parse('Hello from {firstName} {middleName} {lastName}!', $v);
echo
" \n";
// example with missing variable, overriden by a property
$p->middleName = 'Constant Louis';
echo
$p->parse('Hello from {firstName} {middleName} {lastName}!', $v);
echo
" \n";
// example with object properties
$p->firstName = 'Martin';
$p->middleName = 'Luther';
$p->lastName = 'King';
echo
$p->parse('Hello from {firstName} {middleName} {lastName}!');
echo
" \n";

print_r($p);

/* Outputs:
Hello from John Fitzgerald Kennedy!
Hello from Robert Kennedy!
Hello from George {middleName} Washington!
Hello from George Constant Louis Washington!
Hello from Martin Luther King!
Person Object
(
    [firstName] => Martin
    [lastName] => King
    [absintVar:protected] => Closure Object
        (
            [this] => Person Object
 *RECURSION*
            [parameter] => Array
                (
                    [$key] => <required>
                    [$value] => <required>
                )

        )

    [middleName] => Luther
)
 */
 
?>


Details

Absint adds a template engine to your objects with just a few PHP lines. The PHP trait can access basic template functions from inside the Class.

Use the trait to have your objects render themselves, as a replacement for __toString, or just to feature nice debug/exception messages. Bind the replacement functions at runtime with a custom function. Cut & paste some extended functions, or use the supplied extra class Absinter to act as a template engine on its own.

The basics.

The method parse(text, vars) accepts a template text and replacement variables. The method bind(function) allows for a function to be bound on runtime replacement, i.e. htmlspecialchars and such. The method getPublicVars(object) retrieves the public members of the Class and adds them to the known variables.

Trait.

Just add the Trait (USE statement) to any object. All public members are now treated as template variables, no need to retrieve them first. Supply a string (template text) and an array of additional variables to the parse method, and retrieve the text.

Class.

An Absinter class is also available to act as a template class on its own. It uses the Absint Trait and can memorize the replacements variables: method set(key, value). It contains an additional method tie(object) which takes the public members of the object and adds them to the collection of variables.

Extended.

The PHP Trait has room for pasting a few extended methods, named absint.xxxx.inc.php. These extended methods are part of absint, and optional to use: * The method render accepts an external file by name and calls parse. * The method display accepts an external PHP file by name, calls render, and evaluates securely using eval() the results.

Function.

An absintf() function is also provided that does the same as the absint->parse method. It's a substitute for PHP native functions like sprintf() and vsprintf(). For people that love minimal tech or dislike a class. An absintr() function is provided to show the simplicity of templating. It uses the native PHP function strtr().

Precedence.

Three versions of replacement variables are possible: (1) as argument to the method calls, (2) the public members, and (3) the memorizing version by using the method set (class only). Precedence is (1) - (3) - (2). This means that (2) cannot overwrite (3) cannot overwrite (1). Please note that (3) is not existing in the trait; only in the class.

Test.

A simple test (1.php) is available to check the behaviour of the PHP function get_object_vars($object).

This package is meant to study or trial. It is free. Be inspired by it. Share your thoughts, ideas, usage(s) on the forum.


  Files folder image Files (10)  
File Role Description
Files folder imageextended (2 files, 1 directory)
Plain text file absint.trait.php Class PHP trait
Plain text file Absinter.class.php Class PHP class
Plain text file demo.class.php Example demo source
Plain text file demo.trait.php Example demo source
Plain text file readme.md Doc. explanation
Plain text file 1.php Aux. test get_object_vars

  Files folder image Files (10)  /  extended  
File Role Description
Files folder imagefunctions (2 files)
  Plain text file absint.display.inc.php Class extension 'display'
  Plain text file absint.render.inc.php Class extension 'render'

  Files folder image Files (10)  /  extended  /  functions  
File Role Description
  Plain text file absintf.function.inc Aux. aux function
  Plain text file absintr.function.inc Aux. aux function

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  
 0%
Total:76
This week:0
All time:10,170
This week:47Up