An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

        

# TemplateEngine
[![Packagist](https://img.shields.io/packagist/v/carry0987/template-engine.svg?style=flat-square)](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 Redis

Save 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
logo
```
Output:
```html
logo
```

## Functions
#### **`echo`** function
html
```html
{$value}
```
PHP
```php

```

#### **`assign variable`** function
>Note: don't put any php script into **`block`** tag

html
```html

html content

```
PHP
```php
html content

EOF;
?>
```

#### **`if`** function
html
```html

statement1

statement2

statement3

```
PHP
```php

statement1

statement2

statement3

```

#### **`loop`** function (without key)
html
```html

username

```
PHP
```php

username

```

#### **`loop`** function (with key)
html
```html

{$key} = {$value}

```
PHP
```php
$value) {?>
=

```

#### **`eval`** function
html
```html

{$value}
```
PHP
```php


```

## **`PRESERVE`** mark
html
```html

html content

/*{PRESERVE}*/

const value = 1+2;
document.querySelector('span').innerHTML = `Value: ${value}`;

/*{/PRESERVE}*/
```
PHP
```php
html content

const value = 1+2;
document.querySelector('span').innerHTML = `Value: ${value}`;

```