PHP Classes

PHP Template Compiler: Template engine that compiles logic into PHP code

Recommend this page to a friend!
  Info   View files Example   View files View files (26)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 249 This week: 1All time: 7,928 This week: 560Up
Version License PHP version Categories
template-compiler 1.0.1The PHP License5.4PHP 5, Templates, Code Generation
Description 

Author

This package is a template engine that compiles logic into PHP code.

It can take a given template file and parse it to generate a PHP script that will render the template output after processing the variable values and logic controls.

Currently it supports logic controls like: FOREACH, IF, ELSE, ELSEIF, SWITCH, CASE, BREAK, DEFAULT, INCLUDE, CONTINUE.

The processed output can be compressed and cached.

Picture of Victor V. Nabatov
  Performance   Level  
Name: Victor V. Nabatov <contact>
Classes: 2 packages by
Country: Russian Federation Russian Federation
Age: ???
All time rank: 268876 in Russian Federation Russian Federation
Week rank: 411 Up22 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php
/**
 * PHP Template Compiler v2.0.
 */

ini_set('display_errors', 1); # It is for testing

/** Alias for DIRECTORY_SEPARATOR */
define ('DS', DIRECTORY_SEPARATOR);
/** Alias for line feed */
define ('LF', PHP_EOL);
/** Root directory */
define ('ROOT', '.'.DS);
/** Directory for caching compiled templates */
define ('CACHE', ROOT.'cache'.DS);
/** Translations */
define ('LANGUAGES', ROOT.'languages'.DS);
/** Example styles */
define ('STYLES', ROOT.'css'.DS);
/** Example templates */
define ('TEMPLATES', ROOT.'templates'.DS);
/** Generator */
define('GENERATOR', 'template-compiler v2.0');
/** Copyright */
define('COPYRIGHT', '&copy; 2016 Greenray');

require_once
'template.class.php';

$options['cache_page'] = TRUE;
$options['cache_css'] = FALSE;
$options['expired'] = 3600;
$options['compact'] = TRUE;
$options['language'] = 'english';
$options['extension'] = '.tpl.php';

$locales = glob(LANGUAGES.'*.php');
if (!empty(
$_POST['language'])) {
    if (
in_array(LANGUAGES.$_POST['language'].'.php', $locales, TRUE)) {
       
$options['language'] = $_POST['language'];
       
$options['expired'] = 0;
    }
}

$languages = [];
foreach (
$locales as $key => $filename) {
   
$file_info = pathinfo($filename);
   
$languages[$key]['language'] = $file_info['filename'];
    if (
$languages[$key]['language'] === $options['language']) {
       
$languages[$key]['selected'] = TRUE;
    }
}
unset (
$filename);

$menu = json_decode(file_get_contents('menu.json'), TRUE);
$content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam arcu ligula, faucibus eu imperdiet eu, bibendum sit amet augue. Sed turpis sem, interdum sit amet egestas a, mattis non libero. Suspendisse tristique nisi sed justo accumsan vel mattis nulla fermentum. Etiam varius est id mi fermentum aliquam.';

$TEMPLATE = new TEMPLATE('main', $options);
$TEMPLATE->set('class', 'content');
//$TEMPLATE->set('title', 'Included content');
$TEMPLATE->set('title', 'Content');
$TEMPLATE->set('size', '2');
$TEMPLATE->set('content', $content);
$TEMPLATE->set('languages', $languages);
$TEMPLATE->set('menu', $menu);

echo
$TEMPLATE->parse();


Details

Template Compiler.

Feachures

Handles special tags and replaces them with commands of the php interpreter. Transforms template variables to php variables. 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.

To control the compilation uses the HTML comment tags. This makes the template friendly and intuitive even for the inexperienced designer.

Requirements

This program requires PHP 5.4+

List of template functions

<!-- FOREACH $array -->
<!-- FOREACH $array.index -->
<!-- CONTINUE -->
<!-- IF $variable -->
<!-- IF $var1 == $var2 -->
<!-- IF !empty($varable) -->
<!-- ELSEIF $variable -->
<!-- ELSEIF $var1 == $var2 -->
<!-- ELSEIF !empty($variable) -->
<!-- ELSE -->
<!-- SWITCH $variable -->
<!-- CASE $variable -->
<!-- BREAK -->
<!-- DEFAULT -->
<!-- INCLUDE filename -->
<!-- END --> Closes FOREACH IF SWITCH

Template constants and variables

{CONSTANT} means CONSTANT

$variable means $variable

$variable.index means $variable['index']

[$variable] or [$variable.index] is used in special case, ex. style="width:[$variable.width]px"

\__$variable\__ or \__Any words\__ when you need translations into another language

Example

<div class="header center">__Example__</div>
    <!-- INCLUDE menu -->
<div class="page">
...
</div>

This is template to include

<div class="main-menu center">
    <ul class="menu">
    <!-- FOREACH $menu -->
        <li>
            <a href="$menu.link">__$menu.name__</a>
            <!-- IF !empty($menu.sections) -->
                <ul>
                <!-- FOREACH $menu.sections -->
                    <li><a href="$sections.link" style="width:[$sections.width]px">__$sections.title__</a></li>
                <!-- END -->
                </ul>
            <!-- END -->
        </li>
    <!-- END -->
    </ul>
</div>

Result

<div class="header center">Example</div>
<div class="main-menu center">
    <ul class="menu">
        <li>
            <a href="#">Index</a>
        </li>
        <li>
            <a href="#">Publications</a>
                <ul>
                    <li>
                        <a href="#" style="width:100px">News</a>
                    </li>
                    <li>
                        <a href="#" style="width:200px">Articles</a>
                    </li>
                </ul>
            </li>
    </ul>
</div>
<div class="page">
...
</div>

After finishing (removing newlines) the data file will be placed in one line.

The original code: (https://github.com/Greenray/template-compiler).

COPYRIGHT AND LICENSE

Copyright (C) 2016 Victor Nabatov <greenray.spb@gmail.com>

This program is free software. You can redistribute it and/or modify it under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License . This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

??? ????????? ???????? ?????????. ?? ?????? ?????????????? ?/??? ?????????????? ?? ? ???????????? c ????????? Creative Commons Attribution-ShareAlike 4.0 International License . ??? ????????? ???????????????? ? ??????? ??? ??? ????? ????????, ?? ??? ?????-???? ????????; ???? ??? ??????????????? ???????? ???????????? ???????? ??? ??????????? ??? ?????????? ????.


  Files folder image Files  
File Role Description
Files folder imageapi (6 files, 2 directories)
Files folder imagecache (1 file)
Files folder imagecss (3 files)
Files folder imagelanguages (3 files)
Files folder imagetemplates (3 files)
Accessible without login HTML file CC-BY-SA-LICENSE4.0.html Doc. Documentation
Accessible without login Plain text file index.php Example Example script
Accessible without login Image file LT35.png Icon Icon image
Accessible without login Plain text file menu.json Data Auxiliary data
Accessible without login Plain text file readme.md Doc. Documentation
Plain text file template.class.php Class Class source

  Files folder image Files  /  api  
File Role Description
Files folder imageresources (1 file)
Files folder imagetemplate (3 files)
  Accessible without login HTML file deprecated.html Doc. Documentation
  Accessible without login HTML file index-all.html Doc. Documentation
  Accessible without login HTML file index.html Doc. Documentation
  Accessible without login Plain text file style.css Data Auxiliary data
  Accessible without login HTML file todo.html Doc. Documentation
  Accessible without login HTML file tree.html Doc. Documentation

  Files folder image Files  /  api  /  resources  
File Role Description
  Accessible without login Image file nested.png Icon Icon image

  Files folder image Files  /  api  /  template  
File Role Description
  Accessible without login HTML file package-summary.html Doc. Documentation
  Accessible without login HTML file package-tree.html Doc. Documentation
  Accessible without login HTML file template.html Doc. Documentation

  Files folder image Files  /  cache  
File Role Description
  Accessible without login Plain text file fad58de7366495db4650cfefac2fcd61 Data Auxiliary data

  Files folder image Files  /  css  
File Role Description
  Accessible without login Image file logos.jpg Icon Icon image
  Accessible without login Plain text file normalize.css Data Auxiliary data
  Accessible without login Plain text file style.css Data Auxiliary data

  Files folder image Files  /  languages  
File Role Description
  Accessible without login Plain text file english.php Aux. Auxiliary script
  Accessible without login Plain text file russian.php Aux. Auxiliary script
  Accessible without login Plain text file spanish.php Aux. Auxiliary script

  Files folder image Files  /  templates  
File Role Description
  Accessible without login Plain text file content.tpl.php Aux. Auxiliary script
  Accessible without login Plain text file main.tpl.php Aux. Auxiliary script
  Accessible without login Plain text file menu.tpl.php Aux. Auxiliary script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:249
This week:1
All time:7,928
This week:560Up