PHP Classes

File: replacerDemo.php

Recommend this page to a friend!
  Classes of Satish B   Populate template with JSON data   replacerDemo.php   Download  
File: replacerDemo.php
Role: Example script
Content type: text/plain
Description: Demo php explaining the class usage
Class: Populate template with JSON data
Replace templates using values from a JSON object
Author: By
Last change:
Date: 2 years ago
Size: 2,163 bytes
 

Contents

Class file image Download
<?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/>";