Phplib template Chinese Document

Source: Internet
Author: User
// From: http://www.4kiki.net/php_lib_template/

Phplib template Chinese document first version IwindTranslation (updated on November 24)
Chapter 10. Template)

Directory

Template class instance variables

Template instance method
Callable instance method

Internal instance method
Template application example

Note:If you think it is like fasttemplates, you should read it carefully. In fact, it is not.

This template class allows you to retain HTML code in some external files. These files do not contain PHP code and only contain fields to replace: is the "Reference" of the variable mentioned below "). This class provides some functions that allow you to use any string to fill in the field to be replaced.

 

Note:The version of this document is no longer maintained. Please refer to the notes section in the template. inc source file for the most reliable document.

Template class instance variables

Callable instance variables

Classname String. Class sequence help: Class Name.
Debug Integer. identifier: set to 1 to observe the assignment of all variables. Set to 2 to observe the callable variables. Set to 4 to observe internal function calls.
Unknowns "Keep", "comment", "Remove" (default. determines how to process variable names that are not yet processed in the template in the output stage. if it is set to "keep", these variable names will remain intact. if it is set to "comment", the variable name that has not been processed will be
The comment that is converted to HTML. If it is set to "Remove", the variable names that have not been processed will be quietly deleted (this is also the default situation ).
Halt_on_error = "yes" Is "yes" (default), "Report", "no. the template error handling method is determined. if it is set to "yes" (default), errors will be reported and program execution will be interrupted. if it is set to "Report", errors will also be reported, but the program continues to run and "false" is returned ". if it is set to "no", the error will be blocked, and "false" will be returned for the program execution result"
Last_error = "" The last error message is stored in this variable.

Internal instance variables

File Character string hash. A conversion table can convert the variable name to the corresponding file name.
Root String (path name). The main directory to which the template file is loaded.
Varkeys Character string hash. A conversion table can convert the variable name to a corresponding regular expression.
Varvals Character string hash. A conversion table can convert variable names into alternative values corresponding to their respective varkeys.

Template instance method

Instance call Method

Template ($ root = ".", $ unknowns = "Remove ")

Constructor. There can be two optional parameters. The first parameter sets the directory of the template, and the second parameter sets the processing method of unknown variables.

Set_root ($ root)

This function checks whether $ root is a valid directory and sets the directory where this template is stored as a high-profile path.

Set_unknowns ($ unknowns = "Remove ")

This function sets the processing method for unprocessed variable names. it must be one of "Remove", "comment", or "keep. if it is set to "keep", these variable names will remain intact. if it is set to "comment", all the variable names that have not been processed will be converted into HTML comments at the same time. if it is set to "Remove", the variable names that have not been processed will be quietly deleted (this is the default situation ).

Set_file ($ varname, $ filename = "")

This function defines a file name for the initial value of a function. It can be called using a $ varname/$ filename pair or a series of $ varname/$ filename pairs. these files are loaded only when necessary.

Set_block ($ parent, $ varname, $ name = "")

The variable $ parent can contain a variable block named $ varname. this function deletes the block from $ parent and replaces it with a variable named $ name. if $ name is ignored, it is assumed to be the same as $ varname.

Set_var ($ varname, $ value = "")

This function sets the initial value of a variable. You can use a $ varname/$ value pair or a series of $ varname/$ value pairs to call it.

SUBST ($ varname)

This function returns the value of a variable named $ varname. All values defining the variable have been filled in. the result string is not the final result, but does not completely process the variable.

Psubst ($ varname)

This is the short form of Print $ this-> SUBST ($ varname.

Parse ($ target, $ varname, $ append = false)

This function replaces the values of all Defined variables in the name of $ varname, and stores or attaches them to the results with $ target as the variable name.

$ Append is blocked if $ varname is an array of variable names. variables named as $ varname are continuously replaced. The result of each replacement step is included in $ target. final substitution result
It can be obtained in the variable named $ target, which can be considered as an intermediate processing link of the next $ varname.

Pparse ($ target, $ varname, $ append = false)

This is the abbreviated form of Print $ this-> parse (...).

Get_vars ()

Returns all sequences of defined values. Each value uses its name as the key.

Query_id ()

Returns the value of the variable named $ varname. if $ varname corresponds to a file and the file has not been loaded, the report variable is empty. when called using the variable name array, a sequence of values is returned, with their names as keys.

Get_undefined ($ varname)

This function returns a series of unprocessed variable names (that is, a sequence containing $ A [$ name] = $ name) in the form of $ varname without a key name ).

Finish ($ Str)

This function returns the final version of $ STR, that is, the processing method for incomplete processing will be applied to $ Str.

P ($ varname)

This function outputs the value of the last version of the variable named $ varname.

Get ($ varname)

This function returns the value of the last version of the variable named $ varname.

Haltmsg ($ MSG)

This function can be overwritten in your template subclass. It can output information for calling.


Internal instance method

Filename ($ filename)

When a relative path is called, the function returns a path name with the appropriate directory name extracted from $ this-> root. if it is an absolute path, it is used without any change.

The result file name must exist. Otherwise, an error occurs.

Varname ($ varname)

This function constructs a variable name expression based on the given variable name }).

LoadFile ($ varname)

If a variable is undefined or empty and corresponds to a file, the corresponding file will be loaded and the file content will be assigned the value of the variable.

Halt ($ MSG)

Whenever an error occurs, this function will be invoked and handled in the way defined by $ this-> halt_on_error.


Template application instance

This template class manages the collection of some variables, all of which are text strings. these strings may contain references to other variables in the form of {Variable. when analyzing or replacing a variable, the reference of a variable is replaced by the value of that variable. for example

<?php
$t = new Template;

$t->set_var("a", "defined as hugo");
$t->set_var("b", "the value of a is {a}");

print $t->subst("b")
?>

Output "the value of A is defined as Hugo ".

The value of a variable can be defined by manually calling set_var ('name "," value ");, or by calling set_file (" name "," filename. ihtml "); defined from a file. in the latter case, when necessary (as late as possible) the file content is loaded and set as the variable value.

The third way to define the variable value is to call set_block ("parent", "Block", "name ");. in this case, variables named "parent" are treated as <! -- Begin block --> to start with <! -- End block --> is the block to be searched. the string is deleted from the variable "parent" and assigned to a variable named "Block. the variable reference corresponding to "name" in "parent" is replaced. if you can select "name"
Leave it blank. "Block" will be used for replacement.

For example, if you write

<?php
$t = new Template;

$t->set_var("a", "front matter

<!-- BEGIN b -->
this is block b
<!-- END b -->

end matter");
$t->set_block("a", "b", "bb");
?>

The variable "a" is defined as "front matter {bb} end matter", and the variable "B" is used as a block. all this makes it clearer when you set the internal variable debug to 7 in the following example.

Use a template class directly or define a subclass of a template class if necessary.

Define a template file named page. ihtml as follows.

<body bgcolor="#ffffff">
<table border=1 cellpadding=4 cellspacing=0 bgcolor="#eeeeee">
<tr>
<td colspan=2></tr>
<tr>
<td>{OUT}</td>
<td>Content<br>{UNDEFINED_VARIABLE}</td>
</tr>
</table>
</body>

This file contains a reference to the corresponding variable pagetitlet and a reference named out. the reference of another variable named undefined_variable is not parsed. another template file named box. ihtml contains a block named row and contains three variable references. {Title}, {num} and {bignum }:

<!-- start box.ihtml -->
<table border=1 bgcolor="#cccccc" cellpadding=4 cellspacing=0>
<tr>
<td colspan=2><b>{TITLE}</b></td>
</tr>
<!-- BEGIN row -->
<tr>
<td>{NUM}</td>
<td>{BIGNUM}
</tr>
<!-- END row -->
</table>
<!-- end box.ihtml -->

The following PHP file demonstrates how to use these templates:

<? PHP
/* Introduce the template class */
Include ("template. Inc ");

/* Generate an Instance Object of the template class with the required parameters */
$ T = new template ("/home/Kris/www/test.koehntopp.de/pages/template", "keep ");
/* $ T-> DEBUG = 7; * // * activate all debugging operations */

/* Define two variables from the file */
$ T-> set_file (Array (
"Page" => "Page. ihtml ",
"Box" => "box. ihtml "));

/* Define the variable contained in another variable */
$ T-> set_block ("box", "row", "rows ");

/* Manually define two variables */
$ T-> set_var (Array ("title" => "testseite ",
"Pagetitle" => "Hugo "));

for ($i=1; $i<=3; $i++) {
$n = $i;
$nn = $i*10;

/* Assign values to num and bignum */
$ T-> set_var (Array ("num" => $ N, "bignum" => $ NN ));

/* Replace num and bignum in row
* Append the result to rows */
$ T-> parse ("rows", "row", true );
}

/* Replace all variables in the box,
Results are stored in out */
$ T-> parse ("out", array ("box", "page "));

/* Print out */
$ T-> P ("out ");
?>

<HR>
Output undefined variables:
<?php
print @implode(", ", $t->get_undefined("OUT"));
?>

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.