Download the PHP package esi/simple_tpl without Composer
On this page you can find all versions of the php package esi/simple_tpl. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download esi/simple_tpl
More information about esi/simple_tpl
Files in esi/simple_tpl
Package simple_tpl
Short Description Simple TPL - A Simple Template Engine
License GPL-3.0-or-later
Informations about the package simple_tpl
Simple Template Engine - Text based template parser.
Simple Template Engine is a small, simple text-based template parsing engine that works on text replacement.
[!IMPORTANT] The
master
branch currently holds the work-in-progress version3.x
, which is a break from the backward compatibility promise. This will be resolved once3.0.0
is released. Since3.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
Compatible with PHP >= 8.2 and can be installed with Composer.
Usage
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
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.
- Create a
FilesystemStorage
instance with the path/directory to where your templates are located. - 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:
PHP to parse the 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
The DatabaseStorage
implementation allows you to use a database for your templates.
- Create a
PDO
instance with your database details to create a connection. - Create a
DatabaseStorageConfig
with your template tablestableName
,nameField
, andcontentField
. - Create a
DatabaseStorage
instance and pass thePDO
andDatabaseStorageConfig
instances to it. - Pass the
DatabaseStorage
instance to theTemplate
class when creating it.
Let's say the content of the example_template
is the same as in the filesystem example, and your
database table structure is:
Then your code would look something like:
Caching
If you would like to utilize caching for templates, you will need to provide the library a PSR-6 cache implementation. You can view a list of packages that provide this implementation on Packagist.
Whether you use FilesystemStorage
or DatabaseStorage
, you can use caching for either by passing an object that implements \Psr\Cache\CacheItemPoolInterface
.
For example:
About
Requirements
- SimpleTpl works with PHP 8.2.0 or above.
Credits
- Author: Eric Sizemore
- Thanks to all Contributors.
- Special thanks to JetBrains for their Licenses for Open Source Development.
Contributing
See CONTRIBUTING for more information.
Bugs and feature requests are tracked on GitHub.
Contributor Covenant Code of Conduct
See CODE_OF_CONDUCT.md
Backward Compatibility Promise
See backward-compatibility.md for more information on Backwards Compatibility.
Changelog
See the CHANGELOG for more information on what has changed recently.
License
Simple Template Engine's license depends on the version you are using:
v3.0.0
and later is licensed underThe MIT License
.v2.0.1
and earlier is licensed under theGNU GPL v3 License
.
See the LICENSE for more information on the license that applies to this project.
Security
See SECURITY for more information on the security disclosure process.