Application Programming Interface
Namespace Template
Class TEMPLATE
- TEMPLATE
public class TEMPLATE
PHP Template Compiler.
Version | 2.0 | ||
Author | Victor Nabatov <greenray.spb@gmail.com> | ||
Copyright | (c) 2016 Victor Nabatov | ||
License | Creative Commons Attribution-ShareAlike 4.0 International | ||
File | template.class.php | ||
Overview | Handles special tags and replaces them with commands of the php interpreter. It is able recursively execute functions and directives: FOREACH, IF, ELSE, ELSEIF, SWITCH, CASE, BREAK, DEFAULT, INCLUDE, CONTINUE. There is a possibility of compressing and caching the result. Completely separated from php code. Requires PHP 5.4 |
Fields
template.class.php at line 22
private string $code = "";
Template content
template.class.php at line 25
private string $file = "";
Name of the template file that is currently executing
template.class.php at line 28
private array $languages = [];
Languages
template.class.php at line 31
private integer $line = 0;
Current line of the template content
template.class.php at line 34
private array $options = [];
Processing options
template.class.php at line 37
private array $statements = [
"BREAK" => "<?php break;?>",
"CONTINUE" => "<?php continue;?>",
"ELSE" => "<?php } else { ?>",
"DEFAULT" => "<?php default: ?>",
"END" => "<?php } ?>"
];
Template statements
template.class.php at line 40
private array $temp = [];
Temporary variable for code processing
template.class.php at line 43
public array $vars = [];
Template variables
Constructor
template.class.php at line 51
public void construct(array $template, array $options)
include LANGUAGES.$this->options['language'].'.php';
Class constructor.
Parameters | array | $template | Template file |
  | array | $options | Processing options. Default is empty |
Methods
template.class.php at line 73
private void getTemplate (string $template)
Reads template.
Parameters | string | $template | Template filename with full path |
template.class.php at line 99
public void set (mixed $var, mixed $value)
Sets variables (plain or array) for the template.
Parameters | mixed | $var | Variable name |
  | mixed | $value | Variable value |
template.class.php at line 110
public string parse ()
Parses the template file.
Return | string | Parsed template |
template.class.php at line 185
private string parseLine (string $code)
Parses a line of code.
Parameters | string | $code | Line of code |
Return | string | Code transformed in php code |
template.class.php at line 251
private string createVar (string $code)
Creates variable.
Parameters | string | $code | Template basic code |
Return | string | PHP code |
template.class.php at line 270
private string _foreach (string $param)
Trasnforms the FOREACH structure in php code.
Parameters | string | $param | Parameters for FOREACH structure |
Return | string | PHP code |
template.class.php at line 287
private string _if (string $code)
Transforms the IF structure in php code.
Parameters | string | $code | Code for IF structure |
Return | string | PHP code |
template.class.php at line 298
private string _switch (string $param)
Transforms the SWITCH structure in php code.
Parameters | string | $param | Code for the SWITCH structure |
Return | string | PHP code |
template.class.php at line 309
private string _case (string $param)
Transforms the CASE structure in php code.
Parameters | string | $param | Parameter for CASE structure |
Return | string | PHP code |
template.class.php at line 323
private string translate (array $match)
Localization.
Array variable $matches contains:
- $matches[0] = part of template between control structures including them;
- $matches[1] = part of template between control structures excluding them.
Parameters | array | $match | Matches for translation |
Return | string | Parsed string |
template.class.php at line 333
private string value (array $match)
Replaces constants and global variables with their values.
Parameters | array | $match | Matches for constants |
Return | string | Parsed string |
template.class.php at line 345
public mixed getFromCache (string $file)
Gets a data from the cache.
Parameters | string | $file | Page name |
Return | mixed | Page from cache |
template.class.php at line 367
private string compact (string $data)
Compacts compiled template.
Parameters | string | $data | Compiled template |
Return | string | Code without newlines and extra spaces |
template.class.php at line 387
public void toCache (string $file, string $data)
Places the compiled data into cache.
Parameters | string | $file | Page name |
  | string | $data | Page to store in the cache |
template.class.php at line 399
private string randomString (integer $num_chars)
Generates random string.
Parameters | integer | $num_chars | The lenght of string to generate |
Return | string | Generated string |
template.class.php at line 418
public void error (string $msg)
Shows an error message.
Parameters | string | $msg | Error message to output |