PHP Classes

Simple Template Engine: Template engine based on text replacement

Recommend this page to a friend!
  Info   View files Example   View files View files (20)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-08 (3 months ago) RSS 2.0 feedStarStarStar 58%Total: 3,771 All time: 869 This week: 30Up
Version License PHP version Categories
simple_tpl 2.0.1GNU General Publi...8.1.0Templates
Description 

Author

This class implements a simple template engine that works by replacing text.

It can read a template file into a string. Then it searches and replaces all occurrences of the template place holder marks with the values of one or more template arguments.

Picture of Eric Sizemore
  Performance   Level  
Name: Eric Sizemore <contact>
Classes: 15 packages by
Country: United States United States
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Instructions

Please see the included example files within the examples directory.

Example

<?php

declare(strict_types=1);

/**
 * Simple Template Engine
 *
 * @author Eric Sizemore <admin@secondversion.com>
 * @package Simple Template Engine
 * @link http://www.secondversion.com/
 * @version 2.0.1
 * @copyright (C) 2006-2023 Eric Sizemore
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU Public License
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// Include composer autoload file
require_once 'vendor/autoload.php';
use
Esi\SimpleTpl\Template;

$tpl = new Template();

/*
Or if you aren't using via composer, you can use the
following code:

// Include class file and instantiate.
require_once '../src/Template.php';
$tpl = new \Esi\SimpleTpl\Template();
*/

/**
* assign expects an array of:
* variable => value
*
* Variables in your template(s) should be in the form of:
* {variable}
*/
$tpl->assign([
   
'title' => 'Simple Template Engine Test',
   
'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.'
]);

// Parse the template file
$tpl->display('example.tpl');

// It's that simple, really.


Details

Simple Template Engine - Text based template parser.

Scrutinizer Code Quality Code Coverage PHPStan Psalm Security Scan Tests Latest Stable Version Downloads per Month License

Simple Template Engine is a small, simple text-based template parsing engine that works on text replacement.

Installation

Composer

$ composer require esi/simple_tpl

Then, within your project (if not already included), include composer's autoload. For example:

<?php

require_once 'vendor/autoload.php';
use Esi\SimpleTpl\Template;

$tpl = new Template();

?>

Manual

Simply drop Template.php in any project and call include 'src/Template.php';, where 'src/' is the path to where you placed the template engine file(s).

For example:

<?php

include 'src/Template.php';
use Esi\SimpleTpl\Template;

$tpl = new Template();
?>

Examples/Usage

Refer to the /examples/ folder for more information and examples.

There are three main methods of the Template class that are used to parse and display a template. An example is provided with the 'examples' directory. Those methods are 'assign()', 'parse()' and 'display()'. The 'display()' method is a wrapper for 'parse()' with the only difference being that 'display()' will echo the contents of the template instead of returning them as a string.

A Simple Example

PHP Code

<?php

require_once 'vendor/autoload.php';
use Esi\SimpleTpl\Template;

$tpl = new Template();

/
* assign expects an array of:
*     variable => value
*
* Variables in your template(s) should be in the form of:
*     {variable}
*/
$tpl->assign([
    'title'   => 'Simple Template Engine Test',
    'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.'
]);

// Parse the template file
$tpl->display('examples/example.tpl');

?>

Template HTML

<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="content-type" content="text/html" />
	<title>{title}</title>
</head>

<body>

<p>{content}</p>

</body>
</html>

About

Requirements

  • Simple Template Engine works with PHP 8.1.0 or above.

Submitting bugs and feature requests

Bugs and feature requests are tracked on GitHub

Issues are the quickest way to report a bug. If you find a bug or documentation error, please check the following first:

  • That there is not an Issue already open concerning the bug
  • That the issue has not already been addressed (within closed Issues, for example)

Contributing

Simple Template Engine accepts contributions of code and documentation from the community. These contributions can be made in the form of Issues or Pull Requests on the Simple Template Engine repository.

Simple Template Engine is licensed under the GNU GPL v3 license. When submitting new features or patches to Simple Template Engine, you are giving permission to license those features or patches under the GNU GPL v3 license.

Guidelines

Before we look into how, here are the guidelines. If your Pull Requests fail to pass these guidelines it will be declined and you will need to re-submit when you?ve made the changes. This might sound a bit tough, but it is required for me to maintain quality of the code-base.

PHP Style

Please ensure all new contributions match the PSR-2 coding style guide. The project is not fully PSR-2 compatible, yet; however, to ensure the easiest transition to the coding guidelines, I would like to go ahead and request that any contributions follow them.

Documentation

If you change anything that requires a change to documentation then you will need to add it. New methods, parameters, changing default values, adding constants, etc are all things that will require a change to documentation. The change-log must also be updated for every change. Also PHPDoc blocks must be maintained.

Branching

One thing at a time: A pull request should only contain one change. That does not mean only one commit, but one change - however many commits it took. The reason for this is that if you change X and Y but send a pull request for both at the same time, we might really want X but disagree with Y, meaning we cannot merge the request. Using the Git-Flow branching model you can create new branches for both of these features and send two requests.

Author

Eric Sizemore - <admin@secondversion.com> - <https://www.secondversion.com>

License

Simple Template Engine is licensed under the GNU GPL v3 License - see the LICENSE file for details


  Files folder image Files  
File Role Description
Files folder image.github (2 files, 1 directory)
Files folder imageexamples (4 files)
Files folder imagesrc (1 file)
Files folder imagetests (1 file, 1 directory)
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file SECURITY.md Data Auxiliary data

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (3 files)
  Accessible without login Plain text file dependabot.yml Data Auxiliary data
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file ci.yml Data Auxiliary data
  Accessible without login Plain text file psalm.yml Data Auxiliary data
  Accessible without login Plain text file tests.yml Data Auxiliary data

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file customDelimiter.php Example Example script
  Accessible without login Plain text file example.php Example Example script
  Accessible without login Plain text file example.tpl Data Auxiliary data
  Accessible without login Plain text file exampleCustomDelimiter.tpl Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
  Plain text file Template.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imagesrc (1 file)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  src  
File Role Description
  Plain text file TemplateTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:3,771
This week:0
All time:869
This week:30Up
 User Ratings  
 
 All time
Utility:83%StarStarStarStarStar
Consistency:86%StarStarStarStarStar
Documentation:-
Examples:83%StarStarStarStarStar
Tests:-
Videos:-
Overall:58%StarStarStar
Rank:1395