📜 ⬆️ ⬇️

Separate - Template for PHP

image
I present to you the PHP template engine that I have been developing for several years and only recently uploaded to the network.

How did it all start and why do we need another template engine?


Smarty and other popular template engines seemed too cumbersome to me for simple projects and I decided to develop my own template engine. The first version appeared in 2004. In the process of developing Internet projects, I constantly introduced new ideas and other improvements. At the present moment, the template engine consists of just one file and still has a number of unique features.

On the website separate.esud.info you can download the source code. Also there is available detailed documentation of all functions in three languages ​​- Russian, English and German. I hope that the project will interest people and will actively develop in the future. So I put the code in Github at github.com/esud/separate
')
A detailed description can be found at ru.separate.esud.info/documentation
Below I will briefly describe the key features.



I must say in advance that this template engine is relatively fast, but nevertheless I didn’t have a goal to make it the fastest in execution. First of all, I had a goal to design it convenient, simple and unique to use.

The simplest thing is to assign values.
Template (index.htm):
${MY_VARIABLE} 


PHP code:
 require_once './separate/SeparateTemplate.php'; $t = SeparateTemplate::instance()->loadSourceFromFile('./index.htm'); $t->assign('MY_VARIABLE', 'my value'); $t->display(); 


Conclusion:
 my value 


You can generate lists with dynamic information.

Template (index.htm):
 <ul> <!-- BEGIN my_block --> <li>${ROW_NUMBER}</li> <!-- END my_block --> </ul> 


PHP code:
 require_once './separate/SeparateTemplate.php'; $t = SeparateTemplate::instance()->loadSourceFromFile('./index.htm'); for($row = 1; $row <= 3; $row++) { $myBlock = $t->fetch('my_block'); $myBlock->assign('ROW_NUMBER', $row); $t->assign('my_block', $myBlock); } $t->display(); 


Conclusion:
 <ul> <li>1</li> <li>2</li> <li>3</li> </ul> 


One of the unique features is the formatting of values. In this case, the Time formatter converts the Unix-Timestamp value to readable time.

Template (index.htm):
 ${(Time)UNIX_TIMESTAMP} 


PHP code:
 require_once './separate/SeparateTemplate.php'; require_once './separate/formatter/TimeFormatter.php'; $t = SeparateTemplate::instance()->loadSourceFromFile('./index.htm'); $t->assign('UNIX_TIMESTAMP', time()); $t->display(); 


Conclusion:
 08:55 


You can also use your own classes that will format the values. In the example below, the ToUpper formatter converts string information to uppercase.

Template (index.htm):
 ${(ToUpper)MY_VARIABLE} 


PHP code:
 require_once './separate/SeparateTemplate.php'; class ToUpperFormatter extends AbstractValueFormatter { public function formatValue($value) { return strtoupper($value); } } $t = SeparateTemplate::instance()->loadSourceFromFile('./index.htm'); $t->assign('MY_VARIABLE', 'my value'); $t->display(); 


Conclusion:
 MY VALUE 


In addition to SeparateTemplate.assign (...), there are other methods for assigning values. For example, SeparateTemplate.assignForBlock (...). With this method, you can assign a value to a variable that is in a particular block (including further sub-blocks). Variables in other blocks are ignored. The SeparateTemplate.assignForGlobal (...) method assigns values ​​to all variables, regardless of which block they are in.

Additional files can be integrated into the main template file, for example:

Header file (header.htm):
 <html> <body> 


Footer file (footer.htm):
  </body> </html> 


Code of the main template (index.htm):
 <!-- INCLUDE header.htm --> <p></p> <!-- INCLUDE footer.htm --> 


Conclusion:
 <html> <body> <p></p> </body> </html> 


IF-conditions are also supported in the template code:

 <!-- IF '${MY_VARIABLE}' == 'hello' --> HTML ... <!-- END IF --> 


Using the parameters, you can set any settings in the template code itself. In PHP code, all available parameters are read using the SeparateTemplate.getParameters () method.

Template (index.htm):
 <!-- PARAMETER NUMBER_OF_ROWS '100' --> 


PHP code:
 require_once './separate/SeparateTemplate.php'; $t = SeparateTemplate::instance()->loadSourceFromFile('./index.htm'); print_r($t->getParameters()); 


Conclusion:
 Array ( [NUMBER_OF_ROWS] => 100 ) 


PS: This is my first article on habrahabr.ru. If you liked it and you want to support the distribution of the Separate template engine, then I will be very happy if you give it a +1. In the comments I will try to answer all your questions.

Source: https://habr.com/ru/post/170269/


All Articles