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.
composer require carry0987/template-engine
- Support pure html as template
- Support CSS, JS file cache
- Support CSS model cache
- Auto minify CSS cache
- Cache lifetime
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 = new DBController($config);
Save to Redis
// Redis configuration
$redisConfig = array(
'host' => 'redis',
'port' => 6379,
'password' => '',
'database' => 1
);
$redis = new RedisController($redisConfig);
Cache specific part of CSS
html
<link href="{loadcss common.css index}" rel="stylesheet" type="text/css">
You can use variable as specific part
<!--{eval $current_page = 'index'}-->
<link href="{loadcss model.css $current_page}" rel="stylesheet" type="text/css">
CSS
/*[index]*/
.header {
display: block;
}
.link {
color: blue;
}
/*[/index]*/
Output: HTML
<link href="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')}-->
<link href="{loadcss model.css $current_page}" rel="stylesheet" type="text/css">
Or string
, seperate by ,
<link href="{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
<link href="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
<link href="{loadcss common.css}" rel="stylesheet" type="text/css">
Output:
<link href="static/css/common.css?v=Ad0Dwf8" rel="stylesheet" type="text/css">
html
<script src="{loadjs jquery.min.js}" type="text/javascript"></script>
Output:
<script src="static/js/jquery.min.js?v=B22PE8W" type="text/javascript"></script>
html
<img src="{static img/logo.png}" alt="logo">
Output:
<img src="static/img/logo.png" alt="logo">
html
<span>{$value}</span>
PHP
<span><?php echo $value; ?></span>
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;
?>
html
<!--{if expr1}-->
statement1
<!--{elseif expr2}-->
statement2
<!--{else}-->
statement3
<!--{/if}-->
PHP
<?php if(expr1) { ?>
statement1
<?php } elseif(expr2) { ?>
statement2
<?php } else { ?>
statement3
<?php } ?>
html
<!--{loop $array $value}-->
<span>username</span>
<!--{/loop}-->
PHP
<?php foreach($array as $value) {?>
<span>username</span>
<?php } ?>
html
<!--{loop $array $key $value}-->
<span>{$key} = {$value}</span>
<!--{/loop}-->
PHP
<?php foreach($array as $key => $value) {?>
<span><?php echo $key; ?> = <?php echo $value; ?></span>
<?php } ?>
html
<!--{eval $value = 1 2}-->
<span>{$value}</span>
PHP
<?php eval $value = 1 2;?>
<span><?php echo $value; ?></span>
html
<!--{PRESERVE}-->
<span>html content</span>
<!--{/PRESERVE}-->
/*{PRESERVE}*/
<script>
const value = 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>