Phplib class Chinese detailed

Source: Internet
Author: User
Tags control characters define function empty printf regular expression reset scalar root directory
<?php
/*
* Phplib Template 7.4 Chinese version (the deficiencies also please correct me)
* (C) Copyright 1999-2000 netuse GmbH
* Kristian Koehntopp
* Peng Zanqing notes in June 2004, qq:9537075 tel:13787877670
* email:mylovepzq@163.com
*/
/* Here is the definition class template*/
Class Template
{
/* If set, the output parameter * *
var $classname = "Template";
var $debug = false; Whether to debug
var $root = "."; /root is the repository directory for template files
var $file = array (); An array containing all the template file names and template names
var $varkeys = array (); The key name that holds the text element
var $varvals = array (); The value that holds the text element
var $unknowns = "Remove";
/* "Remove" => delete undefined variable "comment" => change undefined variable to annotation "keep" => keep undefined variable * *
var $halt _on_error = "yes";
/* "Yes" => exit "report" => reporting error, continue to run * "no" => ignore error * *
var $last _error = "";
/* Last Error saved here * *
/* Public: constructors
* Root: Template directory
* Unknowns: How to handle unknown variables (translator: variable defined as {name})
*/
/* Here is the definition function template*/
function Template ($root = ".", $unknowns = "Remove")
{
if ($this->debug & 4)
{
echo "<p><b> template:</b> root = $root, unknowns = $unknowns </p>\n";
}
$this->set_root ($root);//default to set the file directory to the same directory
$this->set_unknowns ($unknowns);//unknowns default set to "Remove"
}
/* Here is the function set_root*/
function Set_root ($root)
{
if ($this->debug & 4)
{
echo "<p><b> set root directory:</b> root = $root </p>\n";
}
if (!is_dir ($root))
{
$this->halt ("Set root: $root is not an invalid directory.");
return false;
}
$this->root = $root;
return true;
}
Here is the function set_unknowns, that is, the processing of unknown variables
function set_unknowns ($unknowns = "Remove")
{
if ($this->debug & 4)
{
echo "<p><b> unknown:</b> unknown = $unknowns </p>\n";
}
$this->unknowns = $unknowns;
}
/* Here is the function set_file.......................................................*/
The method adds a value based on the key name provided by $varname in array file
function Set_file ($varname, $filename = "")
{
if (!is_array ($varname))//If varname is an array
{
if ($this->debug & 4)
{
echo "<p><b> settings file:</b> (with scalar) varname = $varname, filename = $filename </p>\n";
}
if ($filename = = "")//If file name is empty, output error
{
$this->halt ("Settings file: variable name $varname filename is empty.");
return false;
}
$this->file[$varname] = $this->filename ($filename);
}
Else
{
Reset ($varname);//The key name of the varname as the key name of the file array
The value of the key name as the value of the file array
while (the list ($v, $f) = each ($varname))
{
if ($this->debug & 4)
{
echo "<p><b>set_file:</b> (with array) varname = $v, filename = $f </p>\n";
}
if ($f = = "")
{
$this->halt ("set_file:for varname $v filename is empty.");
return false;
}
$this->file[$v] = $this->filename ($f);
}
}
return true;
}
This method removes a child template from a parent template file
Load it as a block
and replace it with a different template variable.
/* Public:set_file (array $filelist)
* Comment: Set up multiple template files
* FileList: (handle, filename) array
* Public:set_file (String $handle, String $filename)
* Comment: Set up a template file
* Handle: Handle to File
* FileName: template filename
*/
function Set_block ($parent, $varname, $name = "") {
if ($this->debug & 4) {
echo "<p><b>set_block:</b> parent = $parent, varname = $varname, name = $name </p>\n";
}
if (! $this->loadfile ($parent)) {
$this->halt ("Set_block:unable to load $parent.");
return false;
}
if ($name = = "") {
$name = $varname;//If you do not specify a value for the template variable, use the child template name as the template variable name
}
$str = $this->get_var ($parent);
$reg = "/[\t]*<!--\s+begin $varname \s+-->\s*?\n? (\s*.*?\n?) \s*<!--\s+end $varname \s+-->\s*?\n?/sm ";
Preg_match_all ($reg, $str, $m);
$str = Preg_replace ($reg, "{".) $name} ", $STR);
$this->set_var ($varname, $m [1][0]);
$this->set_var ($parent, $STR);
return true;
}
This method adds a new key-value pair to the varname and Varkeys arrays.
/* Public:set_var (array $values)
* Values: (variable name, value) array
* Public:set_var (String $varname, String $value)
* VarName: Name of the variable to be defined
* Value: Values of variables
*/
function Set_var ($varname, $value = "", $append = False) {
if (!is_array ($varname))//If not array
{
if (!empty ($varname))//If Empty
{
if ($this->debug & 1) {
printf ("<b>set_var:</b> with scalar" <b>%s</b> = '%s ' <br>\n ', $varname, Htmlentities ($ Value));
}
$this->varkeys[$varname] = "/". $this->varname ($varname). " /";
if ($append && isset ($this->varvals[$varname)) {
$this->varvals[$varname]. = $value;
} else {
$this->varvals[$varname] = $value;
}
}
} else {
Reset ($varname);
while (the list ($k, $v) = each ($varname)) {
if (!empty ($k)) {
if ($this->debug & 1) {
printf ("<b>set_var:</b> with array" <b>%s</b> = '%s ' <br>\n, $k, Htmlentities ($v));
}
$this->varkeys[$k] = "/". $this->varname ($k). " /";
if ($append && isset ($this->varvals[$k)) {
$this->varvals[$k]. = $v;
} else {
$this->varvals[$k] = $v;
}
}
}
}
}
Define function Clear_var
function Clear_var ($varname) {
if (!is_array ($varname))//If varname is not an array
{
if (!empty ($varname))//If Empty
{
if ($this->debug & 1) {
printf ("<b>clear_var:</b> with scalar) <b>%s</b><br>\n", $varname);
}
$this->set_var ($varname, "");
}
} else {
Reset ($varname);
while (the list ($k, $v) = each ($varname)) {
if (!empty ($v)) {
if ($this->debug & 1) {
printf ("<b>clear_var:</b> with Array) <b>%s</b><br>\n", $v);
}
$this->set_var ($v, "");
}
}
}
}
/* Here is the function unset_var, delete the definition of the variable * *
function Unset_var ($varname) {
if (!is_array ($varname)) {
if (!empty ($varname)) {
if ($this->debug & 1) {
printf ("<b>unset_var:</b> with scalar) <b>%s</b><br>\n", $varname);
}
unset ($this->varkeys[$varname]);
unset ($this->varvals[$varname]);
}
} else {
Reset ($varname);
while (the list ($k, $v) = each ($varname)) {
if (!empty ($v)) {
if ($this->debug & 1) {
printf ("<b>unset_var:</b> with Array) <b>%s</b><br>\n", $v);
}
unset ($this->varkeys[$v]);
unset ($this->varvals[$v]);
}
}
}
}
Replace the change content in the template file with the operation that determines the content, and the separation of data and display
function subst ($varname) {
$varvals _quoted = Array ();
if ($this->debug & 4) {
echo "<p><b>subst:</b> varname = $varname </p>\n";
}
if (! $this->loadfile ($varname))//Mount template file, stop if there is an error
{
$this->halt ("Subst:unable to load $varname.");
return false;
}
Reset ($this->varvals);
while (the list ($k, $v) = each ($this->varvals)) {
$varvals _quoted[$k] = preg_replace (Array ('/\\\\/', '/\$/'), Array (' \\\\\\ ', ' \\\\$ '), $v);
}
Reads the contents of a file into a string and replaces the known key values in the downlink and returns the result
$str = $this->get_var ($varname);
$str = Preg_replace ($this->varkeys, $varvals _quoted, $STR);
return $str;
}
With subst, just output the results directly
function Psubst ($varname) {
if ($this->debug & 4) {
echo "<p><b>psubst:</b> varname = $varname </p>\n";
}
Print $this->subst ($varname);
return false;
}
Replace the contents of one or more files represented by varname
The varvals array stored at Target as the key value is not in the element or appended to the following
Return value is the same as sub
Function Parse ($target, $varname, $append = False) {
if (!is_array ($varname)) {
if ($this->debug & 4) {
echo "<p><b>parse:</b> (with scalar) target = $target, varname = $varname, append = $append </p>\n" ;
}
$str = $this->subst ($varname);
if ($append) {
$this->set_var ($target, $this->get_var ($target). $str);
} else {
$this->set_var ($target, $STR);
}
} else {
Reset ($varname);
while (the list ($i, $v) = each ($varname)) {
if ($this->debug & 4) {
echo <p><b>parse:</b> (with array) target = $target, I = $i, varname = $v, append = $append </p>\n ";
}
$str = $this->subst ($v);
if ($append) {
$this->set_var ($target, $this->get_var ($target). $str);
} else {
$this->set_var ($target, $STR);
}
}
}
if ($this->debug & 4) {
echo "<p><b>parse:</b> completed</p>\n";
}
return $str;
}
The same parse method, except that the method outputs the result
function Pparse ($target, $varname, $append = False) {
if ($this->debug & 4) {
echo "<p><b>pparse:</b> passing parameters to parse...</p>\n";
}
Print $this->finish ($this->parse ($target, $varname, $append));
return false;
}
Returns an array of all the values in the key-value pairs
function Get_vars () {
if ($this->debug & 4) {
echo "<p><b>get_vars:</b> constructing array of VARs ... </p>\n ";
}
Reset ($this->varkeys);
while (the list ($k, $v) = each ($this->varkeys)) {
$result [$k] = $this->get_var ($k);
}
return $result;
}
Returns the corresponding key based on the key name a duty corresponding to the value
function Get_var ($varname) {
if (!is_array ($varname))//If not array
{
if (Isset ($this->varvals[$varname))//If the variable does not exist
{
$str = $this->varvals[$varname];
} else {
$str = "";
}
if ($this->debug & 2) {
printf ("<b>get_var</b> with scalar" <b>%s</b> = '%s ' <br>\n ', $varname, Htmlentities ($ STR));
}
return $str;
} else {
Reset ($varname);
while (the list ($k, $v) = each ($varname)) {
if (Isset ($this->varvals[$v])) {
$str = $this->varvals[$v];
} else {
$str = "";
}
if ($this->debug & 2) {
printf ("<b>get_var:</b> with array" <b>%s</b> = '%s ' <br>\n, $v, Htmlentities ($STR));
}
$result [$v] = $STR;
}
return $result;
}
}
If the load file fails, returns an error and stops
function get_undefined ($varname) {
if ($this->debug & 4) {
echo "<p><b>get_undefined:</b> varname = $varname </p>\n";
}
if (! $this->loadfile ($varname)) {
$this->halt ("Get_undefined:unable to load $varname.");
return false;
}
Preg_match_all ("/{([^ \t\r\n}]+)}/", $this->get_var ($varname), $m);
$m = $m [1];
Returns an error if a matching text cannot be found
if (!is_array ($m)) {
return false;
}
If a non-empty character in the curly braces is found, its value is used as the key value to form a new array
Reset ($m);
while (the list ($k, $v) = each ($m)) {
if (!isset ($this->varkeys[$v])) {
if ($this->debug & 4) {
echo "<p><b>get_undefined:</b> undefined: $v </p>\n";
}
$result [$v] = $v;
}
}
If the array is not empty, it returns the array, or it returns an error
if (count ($result)) {
return $result;
} else {
return false;
}
}
Completes the final processing of STR, using the attribute unknowns of the class to determine the processing method of the dynamic parts that cannot be processed in the template
function Finish ($STR) {
Switch ($this->unknowns) {
Case "Keep"://remain unchanged
Break
Case "Remove"://Remove all non-control characters
$str = preg_replace ('/{[^ \t\r\n}]+}/', "", $str);
Break
Case "comment"://the HTML annotation in curly braces
$str = Preg_replace ('/{([^ \t\r\n}]+)}/', "<!--Template variable \\1 undefined-->", $str);
Break
}
return $str;
}
The output of a parameter variable to a value in a 诮 array is processed
function P ($varname) {
Print $this->finish ($this->get_var ($varname));
}
The value in the array corresponding to the parameter variable is processed and returned
function Get ($varname) {
return $this->finish ($this->get_var ($varname));
}
Check and supplement the given filename
function filename ($filename) {
if ($this->debug & 4) {
echo "<p><b>filename:</b> filename = $filename </p>\n";
}
if (substr ($filename, 0, 1)!= "/")
If the filename does not start with a slash, the representation is a relative path and complements it to the full absolute path
{
$filename = $this->root. " /". $filename;
}
If the file does not exist
if (!file_exists ($filename)) {
$this->halt ("Filename:file $filename does not exist.");
}
return $filename//returns file name
}
Handles the variable name, changes the sensitive character in the regular expression to an escape character, and adds braces at both ends of the variable name
function VarName ($varname) {
Return Preg_quote ("{". $varname. "}");
}
This method loads the file into a key-value pair according to VarName
function LoadFile ($varname) {
if ($this->debug & 4) {
echo "<p><b>loadfile:</b> varname = $varname </p>\n";
}
if (!isset ($this->file[$varname])//If not specified, return error
{
$varname does not reference a file so return
if ($this->debug & 4) {
echo "<p><b>loadfile:</b> varname $varname does not reference a file</p>\n";
}
return true;
}
if (Isset ($this->varvals[$varname))//If a file with VarName is already loaded, return the truth value directly
{
if ($this->debug & 4) {
echo "<p><b>loadfile:</b> varname $varname is already loaded</p>\n";
}
return true;
}
$filename = $this->file[$varname];//Handle is valid take out the corresponding file name
$str = Implode ("", @file ($filename));//Connect each line of the file to a string
if (empty ($STR))//String null description file empty or not present, return error
{
$this->halt ("Loadfile:while loading $varname, $filename does not exist or are empty.");
return false;
}
if ($this->debug & 4) {
printf ("<b>loadfile:</b> loaded $filename into $varname <br>\n");
}
$this->set_var ($varname, $STR);//If the file is not empty, use $varname as the handle, and STR is the variable name
To add a new key value to a key-value pair
return true;
}
Save the analysis results to a file
function SaveToFile ($dir, $varname) {
$data = $this->finish ($this->get_var ($varname));
$FP =fopen ($dir, "w+");
Fwrite ($fp, $data);
}
Clearing an array of assigned values
function Renew () {
$this->varkeys=array ();
$this->varvals=array ();
$this->file=array ();
}
Error prompts and terminates program operation
function Halt ($msg) {
$this->last_error = $msg;
if ($this->halt_on_error!= "no") {
$this->haltmsg ($msg);
}
if ($this->halt_on_error = = "Yes") {
Die ("<b> termination .</b>");
}
return false;
}
Error prompt
function Haltmsg ($msg) {
printf ("<b> template error:</b>%s<br>\n", $msg);
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.