Last Updated | | Ratings | | Unique User Downloads | | Download Rankings |
2024-05-10 (Yesterday) | | Not yet rated by the users | | Total: 1 This week: 1 | | All time: 11,327 This week: 88 |
|
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. | |
|
|
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.
|
Applications that use this package |
|
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.