PHP version requires PHP 4 and higher. I've not tested it much yet but I've definitely used some functions available since version 4. It should work on all systems and servers that support PHP. If you want to know, it was written and tested on Windows 2000, PHP 4.3.0 on Apache 1.3.26.
ASP version is written in JScript running on IIS server coming with Windows 2000 Pro. VBScript seems to be more common for programming in ASP. I've chosen JScript because it's closer to me then VBScript. Developing VBScript version is at the top of TODO list of this project. If you are able to do it, feel free to join the development team at SourceForge.net.
PHP:
require 'simpleht.php'; or include 'simpleht.php';(see PHP documentation on how to use require na include functions)
ASP:
<!--#include file="simpleht.asp"-->
syntax:
string process(string tmpl_file, mixed var_list [, boolean abs_path])
This method reads content of the given template file, processes it and returns text of the final page. Processing means leaving out comments, replacing variables with their actual values and going though loops.
Parameters:
string tmpl_file - takes path to the template file. The path can be relative or absolute which is determined by the abs_path optional parameter (see description on abs_path parameter).
mixed var_list - this parameter takes list of variables. The values could be either an associative array or instance of varlist object (see varlist object description for more info on creating lists of variables).
boolean abs_path - optional parameter. It determines if the path given in tmpl_file parameter is relative or absolute. If abs_path is true the given path is considered absolute. Default value of this parameter is false, if you leave it out.
process method in fact reads the file, converts the variable list to associative array and passes it to process_text method and then return the result. The process_text method is considered private but you can use it if you need.
void process_text(input_text, variables_array)This method takes text to process in input_text and list of variables in form of associative array. It processes the text but returns nothing. You have to read the result from processed variable, internal variable of the template object (like this: template_object_instance.processed in ASP/JScript or $template_obj_instance->processed in PHP).
syntax:
void set(string var_name, mixed var_value)This method stores any value given in var_value under variable name given in var_name. If the variable already exists, its value is changed to the given one. The passed value in var_value can be of any type. The most common will be string or number. To store data for creating loops it can also take arrays or instances of other varlist objects. To learn more about creating loops see description of loop methods.
General use of associative arrays:
list[var_name] = var_valuewhere list is variable of type array, var_name works as regular index that points to var_value.
The use is similar in PHP and ASP/JScript (PHP variables precede '$' character - $list)
list = new Array(); list["fruit1"] = "apple"; list["fruit2"] = "pear";Now, if you pass list to the process method of the template object, it will replace template variables fruit1 (<== fruit1>) and fruit2 with strings apple and pear.
loop_list[pass][var_name] = var_valueThe pass is numerical value that represents the Nth pass through the loop and the var_name / var_value pair stores data for that current pass. For example it can look like this in 5th cycle of the loop:
loop_list[4]["first_name"] = "John";The loop_list variable is only temporal. You cannot pass it to the process function. To make the loop functional, you have to save the loop_list into the main list with variables. Like this:
list["_names"] = loop_list;
listobj.set("_names", loop_list);where _names identifies the loop and gives it unique name.
syntax:
void loop_set(string var_name, string var_value [, string loop_name])Acts the same way as the set method. The only difference is in the optional loop_name which identifies name of the current loop. Why is it optional will be explained later on example.
syntax:
void loop_save([string loop_name])This method must be called at the end of each loop after setting all variables. It stores previously set variables and prepares for new pass of the cycle.
syntax:
array loop_end([string loop_name])loop_end method finishes setting data for the current loop, frees used resources and returns an array containing variable list of the current loop. This array should be passed to set method of the main varlist or to variable list of parent loop. It can look like this:
list.set("_loop1", list.loop_end("tmp_loop"))
This example will show how using varlist object differs from creating the variable list 'manually'. It will also show the basic usage of the loop methods.
First of all - the template file can look like this:
<html> <head> <title><== page_name></title> </head> <body> <h1><== page_name></h1> <h3><== description></h3> <== _loop_i> <== number_1><br> <== number_2> <blockquote> <== _loop_j> <p><== number_2_1><br> <== number_2_2></p> </== _loop_j> </blockquote> </== _loop_i> </body> </html>
The first example showes how to create list of variables 'manualy' using associative array.
PHP$list["page_name"] = "Random numbers"; $list["description"] = "loops demonstration"; for($i = 0; $i < 5; $i++) { $loop_i[$i]["number_1"] = rand(); $loop_i[$i]["number_2"] = rand(); for($j = 0; $j < 5; $j++) { $loop_j[$j]["number_2_1"] = rand(); $loop_j[$j]["number_2_2"] = rand(); } $loop_i[$i]["_loop_j"] = $loop_j; } $list["_loop_i"] = $loop_i; $t = new Template(); echo $t->Process("numbers.tmpl", $list);ASP/JScript
list = new Array(); list["page_name"] = "Random numbers"; list["description"] = "loops demonstration"; loop_i = new Array(); for(i = 0; i < 5; i++) { loop_i[i] = new Array(); loop_i[i]["number_1"] = Math.random(); loop_i[i]["number_2"] = Math.random(); loop_j = new Array(); for(j = 0; j < 5; j++) { loop_j[j] = new Array(); loop_j[j]["number_2_1"] = Math.random(); loop_j[j]["number_2_2"] = Math.random(); } loop_i[i]["_loop_j"] = loop_j; } list["_loop_i"] = loop_i; t = new Template(); Response.Write(t.Process("numbers.tmpl", list));
This example shows use of the varlist object.
PHP$list = new varlist(); $list->set("page_name", "Random numbers"); $list->set("description", "loops demonstration"); for($i = 0; $i < 5; $i++) { $list->loop_set("number_1", rand(), "loop_i"); $list->loop_set("number_2", rand(), "loop_i"); for($j = 0; $j < 5; $j++) { $list->loop_set("number_2_1", rand(), "loop_j"); $list->loop_set("number_2_2", rand(), "loop_j"); $list->loop_save($j, "loop_j"); } $list->loop_set("_loop_j", $list->loop_end("loop_j"), "loop_i"); $list->loop_save($i, "loop_i"); } $list->set("_loop_i", $list->loop_end("loop_i")); $t = new Template(); echo $t->Process("numbers.tmpl", $list);ASP/JScript
list = new varlist(); list.set("page_name", "Random numbers"); list.set("description", "loops demonstration"); for(i = 0; i < 5; i++) { list.loop_set("number_1", Math.random(), "loop_i"); list.loop_set("number_2", Math.random(), "loop_i"); for(j = 0; j < 5; j++) { list.loop_set("number_2_1", Math.random(), "loop_j"); list.loop_set("number_2_2", Math.random(), "loop_j"); list.loop_save(j, "loop_j"); } list.loop_set("_loop_j", list.loop_end("loop_j"), "loop_i"); list.loop_save(i, "loop_i"); } list.set("_loop_i", list.loop_end("loop_i")); t = new Template(); Response.Write(t.Process("numbers.tmpl", list));
The last example is the same as the previous one but the optional loop_name properites were left out where they are not necessary.
PHP$list = new varlist(); $list->set("page_name", "Random numbers"); $list->set("description", "loops demonstration"); for($i = 0; $i < 5; $i++) { $list->loop_set("number_1", rand(), "loop_i"); $list->loop_set("number_2", rand()); for($j = 0; $j < 5; $j++) { $list->loop_set("number_2_1", rand(), "loop_j"); $list->loop_set("number_2_2", rand()); $list->loop_save($j); } $list->loop_set("_loop_j", $list->loop_end(), "loop_i"); $list->loop_save($i); } $list->set("_loop_i", $list->loop_end()); $t = new Template(); echo $t->Process("numbers.tmpl", $list);ASP/JScript
list = new varlist(); list.set("page_name", "Random numbers"); list.set("description", "loops demonstration"); for(i = 0; i < 5; i++) { list.loop_set("number_1", Math.random(), "loop_i"); list.loop_set("number_2", Math.random()); for(j = 0; j < 5; j++) { list.loop_set("number_2_1", Math.random(), "loop_j"); list.loop_set("number_2_2", Math.random()); list.loop_save(j); } list.loop_set("_loop_j", list.loop_end(), "loop_i"); list.loop_save(i); } list.set("_loop_i", list.loop_end()); t = new Template(); Response.Write(t.Process("numbers.tmpl", list));I know, it looks complicated but I believe it will be helpful, especially when creating loops with many variables. It surely shortens the code in ASP.