Skip to content

Repository files navigation

TemplateEngine

Packagist
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

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

// Database configuration$config = array(
'host' => 'localhost',
'port' => 3306,
'database' => 'template',
'username' => 'root',
'password' => ''
);
$database = newDBController($config);

Save to Redis

// Redis configuration$redisConfig = array(
'host' => 'redis',
'port' => 6379,
'password' => '',
'database' => 1
);
$redis = newRedisController($redisConfig);

Cache CSS & JS File

CSS Cache

Cache specific part of CSS
html

<linkhref="{loadcss common.css index}" rel="stylesheet" type="text/css">

You can use variable as specific part

<!--{eval $current_page = 'index'}--><linkhref="{loadcss model.css $current_page}" rel="stylesheet" type="text/css">

CSS

/*[index]*/
.header {
display: block;
}
.link {
color: blue;
}
/*[/index]*/

Output: HTML

<linkhref="cache/model_index.css?v=Ad0Dwf8" rel="stylesheet" type="text/css">

cache/model_index.css

/* index */
.header{display:block}.link{color:blue}
/* END index */

Also, with array

<!--{eval $current_page = array('index','test')}--><linkhref="{loadcss model.css $current_page}" rel="stylesheet" type="text/css">

Or string, seperate by ,

<linkhref="{loadcss model.css index,test}" rel="stylesheet" type="text/css">

CSS

/*[index]*/
.header {
display: block;
}
.link {
color: blue;
}
/*[/index]*//*[test]*/
.header {
display: inline-block;
}
.link {
color: red;
}
/*[/test]*/

Output: HTML

<linkhref="cache/model_MULTIPLE.css?v=Ad0Dwf8" rel="stylesheet" type="text/css">

cache/model_MULTIPLE.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

<linkhref="{loadcss common.css}" rel="stylesheet" type="text/css">

Output:

<linkhref="static/css/common.css?v=Ad0Dwf8" rel="stylesheet" type="text/css">

JS Cache

html

<scriptsrc="{loadjs jquery.min.js}" type="text/javascript"></script>

Output:

<scriptsrc="static/js/jquery.min.js?v=B22PE8W" type="text/javascript"></script>

Static File

html

<imgsrc="{static img/logo.png}" alt="logo">

Output:

<imgsrc="static/img/logo.png" alt="logo">

Functions

echo function

html

<span>{$value}</span>

PHP

<span><?phpecho$value; ?></span>

assign variable function

Note: don't put any php script into block tag

html

<!--{block test}--><span>html content</span><!--{/block}-->

PHP

<?php$test = <<<EOF<span>html content</span>EOF;
?>

if function

html

<!--{if expr1}-->
statement1
<!--{elseif expr2}-->
statement2
<!--{else}-->
statement3
<!--{/if}-->

PHP

<?phpif(expr1) { ?>
statement1
<?php } elseif(expr2) { ?>
statement2
<?php } else { ?>
statement3
<?php } ?>

loop function (without key)

html

<!--{loop $array $value}--><span>username</span><!--{/loop}-->

PHP

<?phpforeach($arrayas$value) {?>
<span>username</span>
<?php } ?>

loop function (with key)

html

<!--{loop $array $key $value}--><span>{$key} = {$value}</span><!--{/loop}-->

PHP

<?phpforeach($arrayas$key => $value) {?>
<span><?phpecho$key; ?> = <?phpecho$value; ?></span>
<?php } ?>

eval function

html

<!--{eval $value = 1+2}--><span>{$value}</span>

PHP

<?php eval $value = 1+2;?>
<span><?phpecho$value; ?></span>

PRESERVE mark

html

<!--{PRESERVE}--><span>html content</span><!--{/PRESERVE}-->
/*{PRESERVE}*/
<script>constvalue=1+2;document.querySelector('span').innerHTML=`Value: ${value}`;</script>
/*{/PRESERVE}*/

PHP

<span>html content</span>
<script>
const value = 1+2;
document.querySelector('span').innerHTML = `Value: ${value}`;
</script>

About

A lightweight and fast PHP template engine, using Composer, featuring caching abilities, customizable cache lifetime, template inheritance, and support for Redis and MySQL.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages