PHP Classes

Populate template with JSON data: Replace templates using values from a JSON object

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 103 This week: 1All time: 9,739 This week: 560Up
Version License PHP version Categories
ssreplacer 1.0.1GNU General Publi...5PHP 5, Templates
Description 

Author

This class provides a package to replace templates using values from a JSON object.

It can take a template file and a file with JSON object stored as a string and it replaces the marks in the template using values from the JSON object.

The class can process the template and return a string with template parameter values from the JSON object inserted in the marks.

Innovation Award
PHP Programming Innovation award nominee
September 2021
Number 8
Templates are a widespread way to define the format of the output of a page.

Inside each template file, you have marks that you will replace with template parameter values.

Usually, you can set the template parameters using code that calls a template engine component.

This package provides an alternative for processing templates. It can read the template parameters from files that define the parameter values in the JSON format.

Manuel Lemos
Picture of Satish B
  Performance   Level  
Name: Satish B is available for providing paid consulting. Contact Satish B .
Classes: 1 package by
Country: India India
Age: ???
All time rank: 4401300 in India India
Week rank: 411 Up26 in India India Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php
echo "<br/>ssReplacer Class quick Sample";
echo
"<br/>=====================================";
echo
"<br/>Needs data.txt and template01.html present in the folder as this page<br/>";
error_reporting(E_ERROR);
include (
"class.ssReplacer.php");
$i=0;
$i= $i +1;
echo
"<br/>$i> Create new replacer";
$ssr = new ssReplacer();
$cnf = $ssr->config();
echo
"<br/>== ssReplacer's default config setting are <br/>";
$cnf = $ssr->ObjtoString($cnf,2,"html");
echo
"<i>". $cnf. "</i>";
$i= $i +1;
echo
"<br/>$i> You can use config(conf_array) method to alter these settings <br/>";
echo
"<br/>== eg. <i>replacer->config(['showMissing'=>true]);</i>";
$showErr = $ssr->config(['showMissing'=>true]);
echo
"<br/>== it will return revised config or false";

echo
"<br/>";
$i++;
echo
"$i> Load data using dataFile(filename) method. You can also use data(dataJson) <br/>";
$res = $ssr->dataFile('./data.txt');
// echo "<br/>== Data =============================================================<br/>";
// $data = $ssr->data();

$i++;
echo
"$i> Load Template using templateFile(filename) method. You can also use template(template_array) <br/>";
$tm = $ssr->templateFile("./template01.html");
/* echo "<br/>== Template =============================================================".sizeof($arrtmplate);
$arrtmplate = $ssr->template();
for($i=0; $i< sizeof($arrtmplate);$i++){
    echo $arrtmplate[$i];
}
*/
   
// var_dump($arrtmplate);
$i++;
echo
"$i> generate Replacer Object Model (ROM) using generateROM() method.<br/>";
$rom = $ssr->generateROM();
echo
"<br/>== Check replacer->err for any errors<br/>";
// echo "<br/>== ROM Generated ==============================================================<br/>";
// var_dump($rom);
// get result
$i++;
echo
"$i> generate Final result using generateResult() method.";
echo
"<br/>== it returns the results array.<br/>";
$arr = $ssr->generateResult();
/* dump using iterating thru array
for($i=0;$i <sizeof($arr);$i++){
    echo $arr[$i];
}
*/

echo "<br/>====== Begin result ======= </br>";
echo
implode($arr);
echo
"<br/>====== End result ======= </br>";
echo
"<br/>";



Screenshots  
  • replacerDemo.png
  • replacerDemo2.png
  Files folder image Files  
File Role Description
Plain text file class.ssReplacer.php Class replacer class
Accessible without login Plain text file data.txt Data Sample data
Accessible without login Plain text file replacerDemo.php Example Demo php explaining the class usage
Accessible without login HTML file template01.html Data sample template

 Version Control Unique User Downloads Download Rankings  
 0%
Total:103
This week:1
All time:9,739
This week:560Up