PHP Classes

SIREN PHP Templating Library: Template engine featuring recursion and nesting

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 100 All time: 9,807 This week: 560Up
Version License PHP version Categories
siren 2.2.4Custom (specified...7Templates, Parsers, PHP 7
Description 

Author

This package provides a template engine featuring nesting and inclusion.

It provides one base template engine processing class that can parse templates and replace variables with support of recursion.

There is also a sub-class that can also process templates that include other template files and supports nesting of template blocks.

Picture of wim niemans
  Performance   Level  
Name: wim niemans <contact>
Classes: 6 packages by
Country: The Netherlands The Netherlands
Age: ???
All time rank: 345989 in The Netherlands The Netherlands
Week rank: 170 Up5 in The Netherlands The Netherlands Up
Innovation award
Innovation award
Nominee: 2x

Example

<?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";

?>


Details

SIREN is a base PHP library to do templating in one go. It has a simple, sufficient API, performs recursive replacement, and uses an intuitive syntax. That's all you need.

syntax: {[$|?]var} where var  is [text]{var}[text]  
                   where text is '-._[\w\d\]'

SIREN features a (key, value) array of variables and exercises PCRE patterns to parse text. The library supports recursion, including files and processing of blocks.

Release 1.
    Allows setFile to refresh the var.
    Added the (internal) vars templateLocation and debugInfo.
    Added Global Variables: varNames starting with '$'.
Release 2.
    Added setVardata() to escape its value from parsing.
    Introduced recursionDepth, default is 'auto'.
    Allow recursionDepth manipulation.
    Added conditional varValues: varNames starting with '?'.

More documentation: full readme, howto use recursion, api docs, license.


  Files folder image Files (12)  
File Role Description
Files folder imageexamples (1 file)
Files folder imagelicense (3 files)
Accessible without login Plain text file code_of_conduct.md Doc. code of conduct
Accessible without login Plain text file demo.php Example demo
Accessible without login Plain text file description.md Doc. short readme
Accessible without login Plain text file readme.md Doc. readme
Accessible without login Plain text file recursion.md Doc. Howto use recursion in SIREN using simple PHP
Accessible without login Plain text file siren.usage.md Doc. api docs
Plain text file Snippet.class.php Class base class for templates.
Plain text file Templet.class.php Class class that extends Snippet.

  Files folder image Files (12)  /  examples  
File Role Description
  Accessible without login Plain text file sqlSnippet.class.php Example example of an application for education / studying purposes only

  Files folder image Files (12)  /  license  
File Role Description
  Accessible without login Plain text file CDDL_1.LICENSE Lic. CDDL license
  Accessible without login Plain text file EUPL-1.2.LICENSE Lic. EUPL license
  Accessible without login Plain text file readme.md Lic. license options

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Reuses Unique User Downloads Download Rankings  
 0%1
Total:100
This week:0
All time:9,807
This week:560Up