How to Boost Your PHP Class Objects with a Minimalist Template Engine using Absint PHP Template Engine Trait - Absint PHP Template Engine Trait package blog

Recommend this page to a friend!
  All package blogs All package blogs   Absint PHP Template Engine Trait Absint PHP Template Engine Trait   Blog Absint PHP Template Engine Trait package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Boost Your PHP...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Updated on: 2021-01-12

Posted on: 2021-01-12

Package: Absint PHP Template Engine Trait

The Absint PHP Template Engine Trait package provides a PHP trait that you can use to add a template engine to any of your PHP classes just by inserting a single line of PHP code to your class code.

Read this short tutorial to learn more about this PHP trait and how can you use the template engine with code samples.

This will help you understand how to improve the implementation of features of your own PHP classes that can benefit from a ready to use template processing engine.




In this article you can learn:

1. What Features the Absint PHP Template Engine Trait Provides

2. How to Use the Absint PHP Template Engine Trait in Practice to Add Template Processing Capabilities to Your Classes

3. What Are the Most Important Functions of the Template Engine

4. How to Use the Trait to make Your Class Render a Template

5. How to Use a Common Set of Template Parameters Every Time a Template is Processed

6. Download or Install this Package with PHP Composer

7. How to Get Support to Use this PHP Trait


1. What Features the Absint PHP Template Engine Trait Provides

The Absint Template Engine implements features like many other template engine like for instance:

- Turn an object into a template

- Do variable substitution

- Optionally evaluate PHP code returning the result

It also provides advanced features like:

- Binding a custom function to process the replaced text by the template engine

- Tieing public members of other objects onto the text.

2. How to Use the Absint PHP Template Engine Trait in Practice to Add Template Processing Capabilities to Your Classes

To use this trait is very simple. Just add the trait in a use statement to the class of your choice like this:

<?php

include_once 'absint.trait.php';

Class 
Person
{
use 
absint;

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

}

Now you can have access to the basic template functions from inside the class.

3. What Are the Most Important Functions of the Template Engine

Here is a summary of the most important functions of the class.

parse(text, key-value-pairs):  parses text for markers like '{variable}' and replaces them with pairs['variable'].

bind(function): add a custom function to the replacement-process, like htmlspecialchars or strtolower, etc.

render(file, pairs):  accept an external fileName and renders it by calling 'parse' with its content.

display(file, pairs): accept an external fileName and renders it by calling 'render'; files with 'php' as extension are securely evaluated for native php code.

4. How to Use the Trait to make Your Class Render a Template

To use this PHP trait to have your objects render themselves just implement your class __toString function so it calls trait parse function.

You can use this to implement a debug feature in your applications by rendering nicely formatted messages to display the contents of your class objects.

Here is an example from the demo.1.php example script that comes with with the class package.

<?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
)
 */
?>

5. How to Use a Common Set of Template Parameters Every Time a Template is Processed

You can also process a template with a common set of template parameters that do not change each time a template is processed.

For that you can use your own class variables and redefine the parse function in a way that it will merge the common template parameter values with parameter values that are passed to your own class parse function.

There is a class named Absinter, that comes with this trait package, that can use this PHP trait but persists the key-value parameter pairs that will be used to merge with template parameters passed to this class parse function.

In addition, the Absinter class provides two extra methods:

set(key, value): persists the key with value as a key-value pair.

tie(object): tie another object by extracting its public members and adding them to the pairs collection.

See demo.2.php example script for some examples.

6. Download or Install this Package with PHP Composer

If you would like to use this PHP trait or the class in the trait package in your projects, you can download the package files by going to the package download page, or you can install it using the PHP composer tool following the instructions in the composer installation page.

7. How to Get Support to Use this PHP Trait

If you got inspired by this package and would like to use it, or you would like to share your opinion about it, just share your thoughts or ideas in the comments below this article

You can also ask me any questions you have about this trait usage in the support forum.




You need to be a registered user or login to post a comment

1,569,983 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

No comments were submitted yet.




  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   Absint PHP Template Engine Trait Absint PHP Template Engine Trait   Blog Absint PHP Template Engine Trait package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Boost Your PHP...  

For more information send a message to info at phpclasses dot org.