PHP Classes

How to Use a PHP Template Engine to Generate HTML or Text from Templates Using the Package Small Template Framework: Output text replacing templates with parameters

Recommend this page to a friend!
  Info   View files Example   View files View files (18)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-05-10 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: 1 This week: 1All time: 11,327 This week: 88Up
Version License PHP version Categories
small-template-frame 1.0GNU General Publi...5PHP 5, Cache, Templates
Description 

Author

This package can output text replacing templates with parameters.

It provides a template class that can read a template file and replace marks on the template text with parameter values.

The package also provides a cache class that can store the generated text strings in cache files.

Later the cache class can read the generated text again from cache files to avoid processing template files when the text output is needed.

Picture of Ákos Nikházy
Name: Ákos Nikházy <contact>
Classes: 10 packages by
Country: Hungary Hungary
Innovation award
Innovation award
Nominee: 1x

Example

<?php
/***********************
    Nikházy Ákos
index.php
The idea is keeping PHP clean from HTML and text.
This small framework does just that. It is pretty
good for small sites.
***********************/

require 'require/head.php';


// ****************
// ini cache
//
// Caching for 10 seconds. If there is a cache that is younger than
// 10 seconds this is the last line that runs in this script.
// You can test this by changing the loop to 100, render the page then
// turn it back to 10 and try rendering it again you will see the 100
// lines in 10 seconds window. Also when you change your code you should
// delete cache files if you work with long Caching times.
//
// If you want to calculate POSTs, GETs or other dynamic stuff you better
// do it before the Cache class. Also based on you dynamic content you
// can do a Cache(0) to re-cache the content at that point.
// ****************

$cache = new Cache(10); // keep cache for X seconds
                                    
// $cache = new Cache(0); this effectively turns off caching for this page

// ****************
// ini template class
//
// Template('the name of template file')
// in the template file you find {{something}}. you can replace
// {{something}} with value by using the tagList[] property
// $template -> tagList['something'] = value property
// ****************
$template = new Template('index'); // this will template the index.html file in the templates folder
$listLineTemplate = new Template('listItem'); // this will template the listItem.html file in the templates folder
$rawTextTemplate = new Template('',' {{texthere}} at rendering'); // This will template that string. The file must be NULL
// ****************
// ini text class
//
// Text('the name of the text file')
// $text->PrintText('textID') method
// returns the text
// ****************
$text = new Text('index');

// ****************
// Calculating stuff before using the main template.
// this is where you put the content together for
// the template
// ****************

$list = '';

for(
$i = 1; $i<=10; $i++)
{
   
   
// you can use smaller templates. This one keeps a simple <li>{{listItemText}}</li> HTML line
    // also here we use the Text class, that loads text from a .json file by id, instead putting text
    // in PHP code.
   
$listLineTemplate -> tagList['item'] = $text->PrintText('listItemText') . $i;
   
   
// ****************
    // We collect the list items as a string in a variable.
    // This line returns the templated HTML string (first paramater is true), but do not work on
    // one lineing it (second parameter is false) as at the end it will be one lined anyway.
    // ****************
   
$list .= $listLineTemplate->Templating(true,false);
   
}

/*
    // a better way for list or even tables. Instead of connecting strings, you put them in an array
        // the Templating method will implode it like this implode('',$array);
    $list = array();
    for($i = 1; $i<=10; $i++)
    {
       
        // you can use smaller templates. This one keeps a simple <li>{{listItemText}}</li> HTML line
        // also here we use the Text class, that loads text from a .json file by id, instead putting text
        // in PHP code.
        $listLineTemplate -> tagList['item'] = $text->PrintText('listItemText') . $i;
       
        // ****************
        // We collect the list items as a string in a variable.
        // This line returns the templated HTML string (first paramater is true), but do not work on
        // one lineing it (second parameter is false) as at the end it will be one lined anyway.
        // ****************
        $list[] = $listLineTemplate->Templating(true,false);
       
    }
*/
// ****************
// Populate the template here
// Examples:
// ****************

$template -> tagList['someText'] = $text->PrintText('someText');

$template -> tagList['content'] = 'This is the content. You should use the Text.class to put text in these parts,
                                    but for the example I write this here. Also this only makes sense if you see
                                    it in the PHP code in index.php'
;
                                   
$template -> tagList['aList'] = $list;

$template -> tagList['otherHTML'] = NULL; // this will replace the {{otherHTML}} tag to the otherHTML file's content

// we templating string same way as files
$rawTextTemplate -> tagList['texthere'] = $text -> PrintText('complete');
$template -> tagList['rawStringExample'] = $rawTextTemplate -> Templating();

$template -> tagList['formatedText'] = $text -> PrintText('formatedText','string',3,3.3);

// ****************
// Show filled template for user and putting it in a cache
// we never reach this point if cache already exists.

// here we use the basic parameters (both true) so we return the templated HTML string in one line.
// then cache it and print it with exit() (happens in the Cache class.
// ****************
$cache -> cache($template -> Templating());

// ****************
// check out the index_no_comment.php file to see how little code does
// how much, to present content to the user.
// ****************


Details

Small-Template-Framework

A small HTML (and other text file) framework for small PHP applications

Setup

Just copy everything to server :D index.php is commented and explains everything.

You can create template files in the templates/html folder following the rules you see in the provided ones. Also, you can template raw strings. This comes handy when you generate template strings on the fly (for example, you have two lists of data from a database and use an ID for template and change it to the data from the other list).

Make it secure

In the cache, class, require, text folder put a .htaccess file with deny from all This is extremely important in the cache folder, as it contains pure HTML of every page/text cached. If you want to cache for example user profile page that is private: you do not want anyone in your cache folder.


  Files folder image Files  
File Role Description
Files folder imagecache (1 file)
Files folder imageclasses (3 files)
Files folder imagecss (3 files, 1 directory)
Files folder imagerequire (1 file)
Files folder imagetemplates (1 directory)
Files folder imagetext (1 file)
Accessible without login Plain text file another-way.php Example Example script
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file index_no_comment.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

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

  Files folder image Files  /  classes  
File Role Description
  Plain text file Cache.class.php Class Class source
  Plain text file Template.class.php Class Class source
  Plain text file Text.class.php Class Class source

  Files folder image Files  /  css  
File Role Description
Files folder imageimg (1 file)
  Accessible without login Plain text file c.php Aux. Auxiliary script
  Accessible without login Plain text file index.php Aux. Auxiliary script
  Accessible without login Plain text file main.css Data Auxiliary data

  Files folder image Files  /  css  /  img  
File Role Description
  Accessible without login Image file icon.png Icon Icon image

  Files folder image Files  /  require  
File Role Description
  Accessible without login Plain text file head.php Aux. Auxiliary script

  Files folder image Files  /  templates  
File Role Description
Files folder imagehtml (3 files)

  Files folder image Files  /  templates  /  html  
File Role Description
  Accessible without login HTML file index.html Doc. Documentation
  Accessible without login HTML file listItem.html Doc. Documentation
  Accessible without login HTML file otherHTML.html Doc. Documentation

  Files folder image Files  /  text  
File Role Description
  Accessible without login Plain text file index.json Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:1
This week:1
All time:11,327
This week:88Up