https://github.com/carry0987/templateengine
A lightweight and fast PHP template engine, using Composer, featuring caching abilities, customizable cache lifetime, template inheritance, and support for Redis and MySQL.
https://github.com/carry0987/templateengine
pdo php8 redis template-engine xxhash
Last synced: 14 days ago
JSON representation
A lightweight and fast PHP template engine, using Composer, featuring caching abilities, customizable cache lifetime, template inheritance, and support for Redis and MySQL.
- Host: GitHub
- URL: https://github.com/carry0987/templateengine
- Owner: carry0987
- License: mit
- Created: 2023-10-06T08:21:12.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-10-09T10:29:27.000Z (7 months ago)
- Last Synced: 2025-03-27T08:48:15.511Z (about 1 month ago)
- Topics: pdo, php8, redis, template-engine, xxhash
- Language: PHP
- Homepage:
- Size: 344 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TemplateEngine
[](https://packagist.org/packages/carry0987/template-engine)
A lightweight and fast PHP template engine, using Composer, featuring caching abilities, customizable cache lifetime, template inheritance, and support for Redis and MySQL.This powerful yet simple template engine provides the flexibility to store and cache your templates in various ways. Whether you're looking to save your templates locally, cache them with longevity in mind, nest template files for complex designs, utilize persistent storage with Redis, or manage templates through MySQL databases, this engine is equipped to handle your needs efficiently and with ease.
## Installation
```bash
composer require carry0987/template-engine
```## Features
- Support pure html as template
- Support CSS, JS file cache
- Support CSS model cache
- Auto minify CSS cache
- Cache lifetime## Usage
You can choose saving version of template file to Database or RedisSave to the database
```php
// Database configuration
$config = array(
'host' => 'localhost',
'port' => 3306,
'database' => 'template',
'username' => 'root',
'password' => ''
);
$database = new DBController($config);
```Save to Redis
```php
// Redis configuration
$redisConfig = array(
'host' => 'redis',
'port' => 6379,
'password' => '',
'database' => 1
);
$redis = new RedisController($redisConfig);
```## Cache CSS & JS File
#### CSS Cache
**Cache specific part of CSS**
html
```html```
You can use variable as `specific part`
```html```
CSS
```css
/*[index]*/
.header {
display: block;
}.link {
color: blue;
}
/*[/index]*/
```
Output:
HTML
```html```
`cache/model_index.css`
```css
/* index */
.header{display:block}.link{color:blue}
/* END index */
```Also, with **`array`**
```html```
Or **`string`**, seperate by `,`
```html```
CSS
```css
/*[index]*/
.header {
display: block;
}.link {
color: blue;
}
/*[/index]*//*[test]*/
.header {
display: inline-block;
}.link {
color: red;
}
/*[/test]*/
```
Output:
HTML
```html```
`cache/model_MULTIPLE.css`
```css
/* index */
.header{display:block}.link{color:blue}
/* END index */
/* test */
.header{display:inline-block}.link{color:red}
/* END test */
```**Directly cache CSS file**
html
```html```
Output:
```html```
#### JS Cache
html
```html```
Output:
```html```
#### Static File
html
```html![]()
```
Output:
```html![]()
```## Functions
#### **`echo`** function
html
```html
{$value}
```
PHP
```php
```#### **`assign variable`** function
>Note: don't put any php script into **`block`** taghtml
```htmlhtml content
```
PHP
```php
html contentEOF;
?>
```#### **`if`** function
html
```htmlstatement1
statement2
statement3
```
PHP
```phpstatement1
statement2
statement3
```
#### **`loop`** function (without key)
html
```htmlusername
```
PHP
```phpusername
```
#### **`loop`** function (with key)
html
```html{$key} = {$value}
```
PHP
```php
$value) {?>
=```
#### **`eval`** function
html
```html{$value}
```
PHP
```php
```## **`PRESERVE`** mark
html
```htmlhtml content
/*{PRESERVE}*/
const value = 1+2;
document.querySelector('span').innerHTML = `Value: ${value}`;/*{/PRESERVE}*/
```
PHP
```php
html contentconst value = 1+2;
document.querySelector('span').innerHTML = `Value: ${value}`;```