Sfhati PHP engine

Sfhati PHP engine

This package implements a template engine that generates PHP compiled files. It takes a given template file and processes it replacing variables and other types of template constructs. The class return the path of PHP script that is the compiled template ready to be executed. The package comes with several plugins that implement variable replacement, conditional sections, loops, and including additional template files.

Download Form Github

install & config

unzip file you already downloaded

include('syntax.php');

include syntax.php file

$syntaxcode = new __SYNTAX();

use same variable name in this line:

$syntaxcode = new __SYNTAX('full_path_folder_here/');

you can add path for folder cache like this

index.php file content

we make this file to use it in your project, so this will help you:)



error_reporting(E_ERROR | E_WARNING | E_PARSE);


// include class file
include('syntax.php'); 
 $syntaxcode = new __SYNTAX();
// include all plugin
$dir = dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR . 'plugin' . DIRECTORY_SEPARATOR;
if ($dh = opendir($dir)) {

    while (($file = readdir($dh)) !== false) {
        if ($file != '.' && $file != '..' && filetype($dir . $file) != 'dir') {
            include($dir . $file);
        }
    }
    closedir($dh);
}

// this function can translate template file and return execute php file content as a variable , 
// so you can make things in out content!
function include_file_template($template_name) {
    global $syntaxcode;
 
    if (end(explode('.', $template_name)) != 'inc')
        $template_name = $template_name . '.inc';
    $export_filename = $syntaxcode->openfile($template_name);
    
    ob_start();   
    include($export_filename);
    return ob_get_clean();
}

// translate template.inc file using include_file_template() function 
$my_simple_tmplt = include_file_template('template/template.inc');

// print out exeute php file 
echo $my_simple_tmplt;
              
          

use code in template file

var plugin option you can used : -var , -sess , -cons
[var:"variable"end var] result
echo $variable;
[var:"variable-var"end var] result
$variable
[var:"variable-sess"end var] result
echo $_SESSION[variable];
[var:"variable-sess-var"end var] result
$_SESSION[variable]
[var:"variable[word]"end var] result
echo $variable[word];
[var:"variable-cons"end var] result
echo variable;


if plugin
use like[if:"expr","statement"end if]
Real Ex.
[if:"[var:"a-var"end var]==1","yes a = 1 [else] no its not 1!"end if] result
<?php if ($a==1) { ?>
    yes a = 1 <?php }else{ ?> no its not 1!     
<?php } ?>


for plugin
use like [for:"id for this loop","start counter","count to","content loop"end for]
Real Ex.
[for:"lop","4","10","this is %lop% , so can use as a var like %lop-var% "end for]
Result
<?php for ($lop = 4; $lop < 10; $lop++) { ?>
    this is <?php echo "$lop"; ?> , so can use as a var like $lop  
<?php } ?>
other Ex. [for:"lop","4","10","
use value as a variable inside if statement!
[if:"%lop-var%==1","yes a = 1 "end if]
"end for]

Result
<?php for ($lop = 4; $lop < 10; $lop++) { ?>
    use value as a variable inside if statement! 
    <?php if ($lop == 1) { ?>
        yes a = 1      
    <?php } ?>
<?php } ?>


each plugin option can use : -sess for session array
use like [each:"array_expression-option","statement"end each]
Real Ex.
[each:"array_expression","
statement use :
%array_expression:key% // print key
%array_expression:val% // print value
%array_expression:val-var% //not print value just for use as a variable
%array_expression:% //print 0/1
%array_expression:#% //print counter row
%array_expression:val[word]% // if value is array you can print item form it
%array_expression:val[word-var]% // if value is array you can use item value as variable
%array_expression:val['word word']% // you can use ' for word contain space chr
"end each]


Result
<?php
$countrnd166arrayexpression = 0;
if (is_array($array_expression))
    foreach ($array_expression as $krnd166arrayexpression => $vrnd166arrayexpression) {
        $countrnd166arrayexpression++;
        $stlrnd166arrayexpression = ($stlrnd166arrayexpression == 1) ? 0 : 1;
        ?>

        statement use :
        <?php echo $krnd166arrayexpression; ?> // print key
        <?php echo $vrnd166arrayexpression; ?> // print value
        $vrnd166arrayexpression //not print value just for use as a variable
        <?php echo $stlrnd166arrayexpression; ?> //print 0/1
        <?php echo $countrnd166arrayexpression; ?> //print counter row
        <?php echo $vrnd166arrayexpression[word]; ?> // if value is array you can print item form it
        $vrnd166arrayexpression[word] // if value is array you can use item value as variable
        <?php echo $vrnd166arrayexpression['word word']; ?> // you can use ' for word contain space chr

    <?php } ?>


include plugin
use like[include:"template_file_without.inc"end include]
Real Ex.

[include:"temp"end include] //get content from template file name temp.inc in same folder of template source
[include:"../temp"end include]
[include:"../temp.inc"end include]
[include:"{template}temp"end include] // this value {template} use in sfhati framework to get template folder
[include:"{plugin}temp"end include] // this value {template} use in sfhati framework to get plugin folder
[include:"{tmp}temp"end include] // this value {template} use in sfhati framework to get tmp folder
[include:"{cache}temp"end include] // this value {template} use in sfhati framework to get cache folder
[include:"{uploaded}temp"end include] // this value {template} use in sfhati framework to get uploaded folder