PHPackages                             esi/simple\_tpl - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. esi/simple\_tpl

ActiveLibrary[Templating &amp; Views](/categories/templating)

esi/simple\_tpl
===============

Simple TPL - A Simple Template Engine

v2.0.1(2y ago)14[2 PRs](https://github.com/ericsizemore/simple_tpl/pulls)GPL-3.0-or-laterPHPPHP ^8.1 &lt;8.5CI passing

Since Nov 30Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/ericsizemore/simple_tpl)[ Packagist](https://packagist.org/packages/esi/simple_tpl)[ GitHub Sponsors](https://github.com/ericsizemore)[ Fund](https://ko-fi.com/ericsizemore)[ RSS](/packages/esi-simple-tpl/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (4)Versions (7)Used By (0)

Simple Template Engine - Text based template parser.
====================================================

[](#simple-template-engine---text-based-template-parser)

[![Build Status](https://camo.githubusercontent.com/10a617fb5f0c819b359ddec111867a432e7d0616ce9f64680ced1c0b0d09d670/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6572696373697a656d6f72652f73696d706c655f74706c2f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ericsizemore/simple_tpl/build-status/master)[![Code Coverage](https://camo.githubusercontent.com/1cf671f6713fcb760dd4f5cd0be07b225757774a181317588501dd420cf9fb0a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6572696373697a656d6f72652f73696d706c655f74706c2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ericsizemore/simple_tpl/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/f3d219e9952e0ffea56aa16acdefb71a179ef0e737b1a8e0fd999635fec9de8f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6572696373697a656d6f72652f73696d706c655f74706c2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ericsizemore/simple_tpl/?branch=master)[![Continuous Integration](https://github.com/ericsizemore/simple_tpl/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/ericsizemore/simple_tpl/actions/workflows/continuous-integration.yml)[![Type Coverage](https://camo.githubusercontent.com/bf4524105692a20a6107b289a86f08056cf8811a503affc1d04cc02bb81c47b1/68747470733a2f2f73686570686572642e6465762f6769746875622f6572696373697a656d6f72652f73696d706c655f74706c2f636f7665726167652e737667)](https://shepherd.dev/github/ericsizemore/simple_tpl)[![Psalm Level](https://camo.githubusercontent.com/e243c0e8f079d982e3947d265eb18cb91eefc2c05bb1d5adfdef64b93211f943/68747470733a2f2f73686570686572642e6465762f6769746875622f6572696373697a656d6f72652f73696d706c655f74706c2f6c6576656c2e737667)](https://shepherd.dev/github/ericsizemore/simple_tpl)[![Latest Stable Version](https://camo.githubusercontent.com/35fcba23b4a95d13a266288dce344152540f1a8db2486643997d97502cb6bebe/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6573692f73696d706c655f74706c2e7376673f6c6162656c3d737461626c65)](https://packagist.org/packages/esi/simple_tpl)[![Development Version](https://camo.githubusercontent.com/a301977d388fb57be3b471b3d7b389f868abd5bbdd95768550ae448c53a56f8b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f64796e616d69632f79616d6c3f75726c3d68747470732533412532462532466769746875622e636f6d2532466572696373697a656d6f726525324673696d706c655f74706c2532467261772532466d6173746572253246636f6d706f7365722e6a736f6e2671756572793d25323425354227657874726127253544253542276272616e63682d616c69617327253544253542276465762d6d617374657227253544266c6162656c3d756e737461626c6526636f6c6f723d253233666634633030)](https://github.com/ericsizemore/simple_tpl/tree/master)[![Downloads per Month](https://camo.githubusercontent.com/3325d958924c0fca21fac1cab4787c736e5bc783783374c574a1f54b56188f79/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f6573692f73696d706c655f74706c2e737667)](https://packagist.org/packages/esi/simple_tpl)

[Simple Template Engine](http://github.com/ericsizemore/simple_tpl/) is a small, simple text-based template parsing engine that works on text replacement.

Important

The `master` branch currently holds the work-in-progress version `3.x`, which is a break from the backward compatibility promise. This will be resolved once `3.0.0` is released. Since `3.x` is under development, it is not recommended to use in a production environment. The public API, implementations, etc., can (and will likely) change.

---

Installation
------------

[](#installation)

Compatible with PHP &gt;= 8.2 and can be installed with [Composer](https://getcomposer.org).

```
$ composer require esi/simple_tpl:^3.0
```

Usage
-----

[](#usage)

### Storage

[](#storage)

There are two storage implementations available: `Storage\FilesystemStorage` and `Storage\DatabaseStorage`. Both storage implementations implement the `Storage\StorageInterface` interface, with only one defined method: `loadTemplate()`.

**NOTE:** If you wish to have a system for editing/saving, deleting or updating the templates themselves, you would need to implement this on your own. This library only searches for templates that have already been created, by name, then parses them with a `key => value` associative array of variables.

#### Filesystem Storage

[](#filesystem-storage)

The `FilesystemStorage` implementation allows you to use regular files for your templates. Your template files are expected to end with the `.tpl` extension. I plan to allow the ability to use different extensions later.

1. Create a `FilesystemStorage` instance with the path/directory to where your templates are located.
2. Pass this instance to the `Template` class when creating it.

Let's say you had a template called `example_template.tpl` within the `./templates/` directory.

Template file:

```
>

    {title}

{content}

```

PHP to parse the template:

```
use Esi\SimpleTpl\Template;
use Esi\SimpleTpl\Storage\FilesystemStorage;

$templateStorage = new FilesystemStorage('./templates/');
$template = new Template($templateStorage);

$template->setTplVars([
    'title' => 'Hello, World!',
    'content' => 'This is a simple template engine.'
]);

echo $template->parse('example_template');
```

When calling `display()` or `parse()`, you only need to provide the file name without extension. For example, if your template file is `mytemplate.tpl`, you would call either of these methods with `mytemplate`.

#### Database Storage

[](#database-storage)

The `DatabaseStorage` implementation allows you to use a database for your templates.

1. Create a `PDO` instance with your database details to create a connection.
2. Create a `DatabaseStorageConfig` with your template tables `tableName`, `nameField`, and `contentField`.
3. Create a `DatabaseStorage` instance and pass the `PDO` and `DatabaseStorageConfig` instances to it.
4. Pass the `DatabaseStorage` instance to the `Template` class when creating it.

Let's say the content of the `example_template` is the same as in the [filesystem example](#filesystem-storage), and your database table structure is:

```
CREATE TABLE IF NOT EXISTS `templates` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL,
    `content` MEDIUMTEXT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `name` (`name`)
)
```

Then your code would look something like:

```
use Esi\SimpleTpl\Template;
use Esi\SimpleTpl\Storage\DatabaseStorage;
use Esi\SimpleTpl\Storage\DatabaseStorageConfig;
use PDO;

$pdo = new PDO('mysql:host=localhost;dbname=templates', 'user', 'password');
$storageConfig = new DatabaseStorageConfig(); // with the example table structure above, the config defaults to 'templates', 'name', 'content'
$templateStorage = new DatabaseTemplateStorage($pdo, $storageConfig);
$template = new Template($templateStorage);

$template->setTplVars([
    'title' => 'Hello, World!',
    'content' => 'This is a simple template engine.'
]);

echo $template->parse('example_template');
```

### Caching

[](#caching)

If you would like to utilize caching for templates, you will need to provide the library a [PSR-6](https://www.php-fig.org/psr/psr-6/) cache implementation. You can view a list of packages that provide this implementation on [Packagist](https://packagist.org/providers/psr/cache-implementation).

Whether you use `FilesystemStorage` or `DatabaseStorage`, you can use caching for either by passing an object that implements `\Psr\Cache\CacheItemPoolInterface`.

For example:

```
$ composer require symfony/cache:^7.2
```

```
use Esi\SimpleTpl\Template;
use Esi\SimpleTpl\Storage\FilesystemStorage;
use Symfony\Component\Cache\Adapter\AbstractAdapter;

$templateStorage = new FilesystemStorage('/path/to/templates');
$template = new Template(
    $templateStorage,
    /**
     * Symfony's AbstractAdapter::createSystemCache() returns the best possible adapter that your runtime supports.
     * Generally, it will create a cache via PHP files (Opcache must be enabled via opcache.enable in php.ini), and chain that with APCu if your system supports it.
     *
     * For more information on symfony/cache's available cache pool (PSR-6) adapters:
     * @see https://symfony.com/doc/current/components/cache/cache_pools.html
     */
    AbstractAdapter::createSystemCache(namespace: 'simple_tpl', defaultLifetime: 300, version: '', directory: sys_get_temp_dir())
);

// ... assign vars, parse /display template, etc ...
```

About
-----

[](#about)

### Requirements

[](#requirements)

- SimpleTpl works with PHP 8.2.0 or above.

Credits
-------

[](#credits)

- Author: [Eric Sizemore](https://github.com/ericsizemore)
- Thanks to [all Contributors](https://github.com/ericsizemore/simple_tpl/contributors).

Contributing
------------

[](#contributing)

See [CONTRIBUTING](./CONTRIBUTING.md) for more information.

Bugs and feature requests are tracked on [GitHub](https://github.com/ericsizemore/simple_tpl/issues).

### Contributor Covenant Code of Conduct

[](#contributor-covenant-code-of-conduct)

See [CODE\_OF\_CONDUCT.md](./CODE_OF_CONDUCT.md)

### Backward Compatibility Promise

[](#backward-compatibility-promise)

See [backward-compatibility.md](./backward-compatibility.md) for more information on Backwards Compatibility.

### Changelog

[](#changelog)

See the [CHANGELOG](./CHANGELOG.md) for more information on what has changed recently.

### License

[](#license)

Simple Template Engine's license depends on the version you are using:

- `v3.0.0` and later is licensed under `The MIT License`.
- `v2.0.1` and earlier is licensed under the [`GNU GPL v3 License`](https://github.com/ericsizemore/simple_tpl/blob/2.x/LICENSE).

See the [LICENSE](./LICENSE.md) for more information on the license that applies to this project.

### Security

[](#security)

See [SECURITY](./SECURITY.md) for more information on the security disclosure process.

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance60

Regular maintenance activity

Popularity5

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 53.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~10 days

Total

3

Last Release

879d ago

PHP version history (2 changes)v2.0.0PHP &gt;=8.1.0

2.x-devPHP ^8.1 &lt;8.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/b7181952b087db2a847c6b90d5cc5e53948e66229ad16acf92ccca3e8767cb92?d=identicon)[ericsizemore](/maintainers/ericsizemore)

---

Top Contributors

[![ericsizemore](https://avatars.githubusercontent.com/u/723810?v=4)](https://github.com/ericsizemore "ericsizemore (377 commits)")[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (322 commits)")

---

Tags

templatetemplate-enginetexttext-replacement

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/esi-simple-tpl/health.svg)

```
[![Health](https://phpackages.com/badges/esi-simple-tpl/health.svg)](https://phpackages.com/packages/esi-simple-tpl)
```

###  Alternatives

[mustache/mustache

A Mustache implementation in PHP.

3.3k44.6M291](/packages/mustache-mustache)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[whitecube/nova-flexible-content

Flexible Content &amp; Repeater Fields for Laravel Nova.

8053.0M25](/packages/whitecube-nova-flexible-content)[mopa/bootstrap-bundle

Easy integration of twitters bootstrap into symfony2

7042.9M33](/packages/mopa-bootstrap-bundle)[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3871.2M](/packages/limenius-react-bundle)[nicmart/string-template

StringTemplate is a very simple string template engine for php. I've written it to have a thing like sprintf, but with named and nested substutions.

2101.7M30](/packages/nicmart-string-template)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
