PHP Classes

TinyBinder: Replace template variables with function results

Recommend this page to a friend!
  Info   View files Example   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-19 (5 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 20 This week: 20All time: 11,133 This week: 5Up
Version License PHP version Categories
tinybinder 1.0MIT/X Consortium ...5PHP 5, Templates
Description 

Author

This class can replace template variables with function results.

It can load a template from a given file.

The class can replace template marks with values from variables passed as an array or functions assigned to variables with the same name as the marks.

Picture of Sheldon Kennedy
Name: Sheldon Kennedy <contact>
Classes: 3 packages by
Country: South Africa South Africa

Instructions

TinyBinder

A super lightweight templating class that merges content, and the results of custom functions, with an HTML file.

Features

  • Variable Replacement: Easily replace variable placeholders in the HTML content by passing in strings or an array.
  • Function Replacement: Replace function placeholders with the results of custom functions stored in a separate file.
  • Debugging Mode: Enable debugging mode to display any placeholders that failed to merge during processing.
  • File or Raw Input: Load HTML content from a file or use raw HTML input.

Installation

Clone the repository or download the TinyBinder.php file and include it in your project.

How to Use the Class

Merge single values:

<?php

$tinyBinder = new TinyBinder('path/to/template.html');
$tinyBinder->addAsset('name', 'John');
$tinyBinder->addAsset('age', '25');
$result = $tinyBinder->getHtml();

Merge multiple values:

<?php

$tinyBinder = new TinyBinder('path/to/template.html');
$tinyBinder->addAssets(['name' => 'John', 'age' => 25]);
$result = $tinyBinder->getHtml();

Debug:

<?php

$tinyBinder = new TinyBinder('path/to/template.html');
$tinyBinder->debug();

Save time by using the static shorthand. This is equivalent to calling addAssets, getHtml and debug:

<?php

$result = TinyBinder::make('path/to/template.html', ['name' => 'John', 'age' => 25], true);

How to Create a Template

1) Add a variable placeholder

Wrap a variable placeholder in double curly braces. In the example below, {{ $name }} will be replaced with the content of the variable $name that you pass in using either the addAsset or addAssets method. The variable name in your HTML template must start with a $ character.

<html>

    {{ $name }}

</html>

2) Add a function placeholder

Wrap a function placeholder in double curly braces. In the example below, {{ @time }} will be replaced with the result of the $time closure in the functions.php file. Functions are merged automatically. The function name in your HTML template must start with a @ character.

<html>

    {{ @time }}

</html>

How to Add a Custom Function

Add a new closure to the included functions.php file:

$time = function() {
    return date('H:i:s');
};

The name of the variable that holds the closure result must match the function placeholder name. The function name in the template must start with a @ character.

<html>

    {{ @time }}

</html>

Methods

  • `make($path, $values, $debug = false)`: Creates a new template instance and initialises it with the provided path and values.
  • `debug($debug = true)`: Enables or disables debugging mode.
  • `addAsset($name, $value)`: Adds a single asset to the template.
  • `addAssets($assets)`: Adds multiple assets to the template.
  • `getHtml()`: Gets the processed HTML content after variable and function replacement.

Contributions

Feel free to contribute or fix any issues. Create a pull request or open an issue for discussion.

License

This project is licensed under the MIT License.

Example

TinyBinder

A super lightweight templating class that merges content, and the results of custom functions, with an HTML file.

Features

  • Variable Replacement: Easily replace variable placeholders in the HTML content by passing in strings or an array.
  • Function Replacement: Replace function placeholders with the results of custom functions stored in a separate file.
  • Debugging Mode: Enable debugging mode to display any placeholders that failed to merge during processing.
  • File or Raw Input: Load HTML content from a file or use raw HTML input.

Installation

Clone the repository or download the TinyBinder.php file and include it in your project.

How to Use the Class

Merge single values:

<?php

$tinyBinder = new TinyBinder('path/to/template.html');
$tinyBinder->addAsset('name', 'John');
$tinyBinder->addAsset('age', '25');
$result = $tinyBinder->getHtml();

Merge multiple values:

<?php

$tinyBinder = new TinyBinder('path/to/template.html');
$tinyBinder->addAssets(['name' => 'John', 'age' => 25]);
$result = $tinyBinder->getHtml();

Debug:

<?php

$tinyBinder = new TinyBinder('path/to/template.html');
$tinyBinder->debug();

Save time by using the static shorthand. This is equivalent to calling addAssets, getHtml and debug:

<?php

$result = TinyBinder::make('path/to/template.html', ['name' => 'John', 'age' => 25], true);

How to Create a Template

1) Add a variable placeholder

Wrap a variable placeholder in double curly braces. In the example below, {{ $name }} will be replaced with the content of the variable $name that you pass in using either the addAsset or addAssets method. The variable name in your HTML template must start with a $ character.

<html>

    {{ $name }}

</html>

2) Add a function placeholder

Wrap a function placeholder in double curly braces. In the example below, {{ @time }} will be replaced with the result of the $time closure in the functions.php file. Functions are merged automatically. The function name in your HTML template must start with a @ character.

<html>

    {{ @time }}

</html>

How to Add a Custom Function

Add a new closure to the included functions.php file:

$time = function() {
    return date('H:i:s');
};

The name of the variable that holds the closure result must match the function placeholder name. The function name in the template must start with a @ character.

<html>

    {{ @time }}

</html>

Methods

  • `make($path, $values, $debug = false)`: Creates a new template instance and initialises it with the provided path and values.
  • `debug($debug = true)`: Enables or disables debugging mode.
  • `addAsset($name, $value)`: Adds a single asset to the template.
  • `addAssets($assets)`: Adds multiple assets to the template.
  • `getHtml()`: Gets the processed HTML content after variable and function replacement.

Contributions

Feel free to contribute or fix any issues. Create a pull request or open an issue for discussion.

License

This project is licensed under the MIT License.


  Files folder image Files  
File Role Description
Files folder imagedemo (2 files)
Files folder imagesrc (2 files)
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Example Example script

  Files folder image Files  /  demo  
File Role Description
  Accessible without login Plain text file RUNME.php Example Example script
  Accessible without login Plain text file template.html Data Demo template

  Files folder image Files  /  src  
File Role Description
  Accessible without login Plain text file functions.php Aux. Auxiliary script
  Plain text file TinyBinder.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:20
This week:20
All time:11,133
This week:5Up