PHP Classes

File: demo.php

Recommend this page to a friend!
  Classes of wim niemans   SIREN PHP Templating Library   demo.php   Download  
File: demo.php
Role: Example script
Content type: text/plain
Description: demo
Class: SIREN PHP Templating Library
Template engine featuring recursion and nesting
Author: By
Last change: added license info
Date: 3 years ago
Size: 3,002 bytes
 

Contents

Class file image Download
<?php
/**
 * This file is an example of an application with Snippet class.
 * Distribution is intended for education / studying purposes only.
 *
 * Copyright [2020] [Wim Niemans <niemans@pbsolo.nl>]
 */

include './Snippet.class.php';
/**
 * @author wim niemans, Rotterdam, Bonn
 * @license EUPL
 */

$snippet = new Snippet();

/**
 * substitute variables in text recusively with their respective values
 * syntax: {var} where var is [text]{var}[text]
 * where text is '-._[\w\d\]'
 *
 * below examples should all output 'The quick brown fox jumps over the lazy dog.'
 */

// (1) demo simple replacements
$snippet->setVar('pangram', 'The {speed} {color} fox {action} over the lazy {pet}.');
$snippet->setVar('speed', 'quick');
$snippet->setVar('color', 'brown');
$snippet->setVar('action', 'jumps');
$snippet->setVar('pet', 'dog');
$snippet->parse('output', 'pangram');
echo
'-1- ' . $snippet->tidy('output') . " \n";

// (2a) demo nimble replacements of varValues
$snippet->setVar('pangram', 'The {1} {2} fox {3} over the lazy {4}.');
$snippet->setVar('1', '{speed}');
$snippet->setVar('2', '{color}');
$snippet->setVar('3', '{action}');
$snippet->setVar('4', '{pet}');
$snippet->parse('output', 'pangram');
echo
'-2- ' . $snippet->tidy('output') . " \n";

// (3) demo recursive replacements of varValues
$snippet->setVar('pangram', 'The {{1}} {{2}} fox {{3}} over the lazy {{4}}.');
$snippet->setVar('1', 'speed');
$snippet->setVar('2', 'color');
$snippet->setVar('3', 'action');
$snippet->setVar('4', 'pet');
$snippet->parse('output', 'pangram');
echo
'-3- ' . $snippet->tidy('output') . " \n";

// (4) demo nimble recursive replacements
$snippet->setVar('pangram', 'The {{br}eed} {col{umn}} fox {ac{et}on} over the lazy {{idol}}.');
$snippet->setVar('br', 'sp');
$snippet->setVar('umn', 'or');
$snippet->setVar('et', 'ti');
$snippet->setVar('idol', 'pet');
$snippet->parse('output', 'pangram');
echo
'-4- ' . $snippet->tidy('output') . " \n";

// (5) demo gotchas still working
$snippet->setVar('pangram', 'The {{br}{own}} {col{umn}} fox {action} over the lazy {{p}e{t}}.');
$snippet->setVar('own', 'eed');
$snippet->setVar('p', 'p');
$snippet->setVar('t', 't');
$snippet->parse('output', 'pangram');
echo
'-5- ' . $snippet->tidy('output') . " \n";

$snippet->clear();

// (6) demo -1- replacement of global variables
$snippet->setVar('pangram', 'The {$speed} {$color} fox {$action} over the lazy {$pet}.');
$speed = 'quick';
$color = 'brown';
$action = 'jumps';
$pet = 'dog';
$snippet->parse('output', 'pangram');
echo
'-6- ' . $snippet->tidy('output') . " \n";

// (7) demo -2- replacement of (recursive) global variables
$snippet->setVar('pangram', 'The {speed} {color} fox {action} over the lazy {pet}.');
$snippet->setVar('speed', '{$speed}');
$snippet->setVar('color', '{$color}');
$snippet->setVar('action', '{$action}');
$snippet->setVarData('pet', '{$pet}');
$snippet->parse('output', 'pangram');
echo
'-7- ' . $snippet->tidy('output') . " \n";

?>