Some basic syntaxes and functions in phplib

Source: Internet
Author: User

Syntax introduction:
Common Methods in phplib include set_file, set_block, set_var, parse, ppasre, p, and get.
Statement: because the system uses phplib, if there is a braces on the page, this will be replaced with a blank space, so when writing this article, use "[[", "]" to replace braces. When we use it, we use braces to make it easier to write articles.
Set_file: used to introduce the template file.
Usage:
Copy codeThe Code is as follows:
$ T-> set_file ("show_main", "main.htm ");
Or
$ T-> set_file (array (
"Show_header" => "header.htm ",
"Show_main" => "main.htm"
));

Set_block: Used to declare a block
Usage:
$ T-> set_block ("show_main", "rowlist", "RL ");
Show_main is the file handle obtained using set_file, and rowlist is the region ID on the template page.
  
Copy codeThe Code is as follows:
  
<Table>
<! -- BEGIN rowlist -->
<Tr>
<Td> [[param] </td>
</Tr>
<! -- END rowlist -->
</Table>

The preceding example uses <tr> </tr> as a block to generate a list of multiple rows in a loop.
Blocks can be nested.
  
Copy codeThe Code is as follows:
  
<Table>
<! -- BEGIN rowlist -->
<Tr>
<! -- BEGIN collist -->
<Td> [[param] </td>
<! -- END collist -->
</Tr>
<! -- END rowlist -->
</Table>

As shown above, this declares a nested block, which is used in the image volume of boeiBlog. If you are interested, you can find it.
For nested templates, we can use
$ T-> set_block ("show_main", "rowlist", "RL"); // the parameters in the table are contained from the beginning to the end, and the last one is an alias, mainly used for block Recognition
$ T-> set_block ("rowlist", "collist", "CL"); // The first parameter is the name of the external block, and the second parameter is your own name, the third is the alias.
Pay special attention to loop through such blocks.
As follows:
  
Copy codeThe Code is as follows:
  
$ T-> set_block ("show_main", "rowlist", "RL ");
$ T-> set_block ("rowlist", "collist", "CL ");
For ($ I = 0; $ I <5; $ I ++)
{
$ T-> set ("CL"); // here we need to clear the appended column loop once, otherwise there will be a bunch of more things
For ($ ii = 0; $ ii <5; $ ii ++)
{
$ T-> set_var ("param", "boeiBlog ");
$ T-> parse ("CL", "collist", true); // The true parameter indicates that this is an append.
}
$ T-> parse ("RL", "rowlist", true); // true here is also table appending.
}

The code above will generate a 5x5 table, and a boeiBlog will appear in each cell.
Set_var: Used to replace variables
In the above Code, $ t-> set_var ("param", "boeiBlog") is to replace the param variable in the template with the boeiBlog string. Of course, you can also replace it with a variable, such:
  
Copy codeThe Code is as follows:
  
$ Curdate = date ("Y-m-d ");
$ T-> set_var ("param", $ curdate );
Set_var also has append attributes, such:
  
Copy codeThe Code is as follows:
  
$ Curdate = date ("Y-m-d ");
For ($ I = 0; $ I <10; $ I ++)
{
$ T-> set_var ("param", "<br>". $ curdate, true );
}

This will generate ten consecutive current dates
Sometimes the append attribute of set_var can be used to replace the block loop.
Set_var can use arrays, such:
  
Copy codeThe Code is as follows:
  
$ T-> set_var (array (
"Param" => "boeiBlog ",
"Title" => "Bai AI network"
));

The template is as follows:
  
Copy codeThe Code is as follows:
  
<Table>
<Tr>
<Td> [[param], [title] </td>
</Tr>
</Table>

Parse: Used to parse Files
After processing all the variables in the template, you can use parse to parse the template. This is the last process of template processing.
For example:
  
Copy codeThe Code is as follows:
  
$ T-> set_file ("show_index", "index.htm ");
$ T-> set_file ("show_main", "main.htm ");
$ T-> set_var ("param", "boeiBlog ");
$ T-> parse ("main", "show_main ");

The template we use may be:
  
Copy codeThe Code is as follows:
  
Main.htm
<Table>
<Tr>
<Td> [[param] </td>
</Tr>
</Table>

If there is another template, its structure is as follows:
  
  
Index.htm
<Div> [[main] </div>
In this case, the Code replaces the variables in main.htmwith boeiblogand then places them in the main field of index.htm. Finally, a table is formed in the <div> </div> label.
After the resolution is complete, the output page is displayed,
P: Used to output pages
For example:
  
Copy codeThe Code is as follows:
  
$ T-> set_file ("show_index", "index.htm ");
$ T-> set_file ("show_main", "main.htm ");
$ T-> set_var ("param", "boeiBlog ");
$ T-> parse ("main", "show_main ");
$ T-> parse ("index", "show_index ");
$ T-> p ("index"); // The entire indexpage is output, and main.htmis inserted into index.htm, so $ t-> p ("main") is not required ");

Pparse: Same as p, it is also used to output pages.
For example:
  
Copy codeThe Code is as follows:
  
The above code can be simplified as follows:
$ T-> set_file ("show_index", "index.htm ");
$ T-> set_file ("show_main", "main.htm ");
$ T-> set_var ("param", "boeiBlog ");
$ T-> parse ("main", "show_main ");
$ T-> pparse ("index", "show_index"); // combine p and parse to complete parsing and output immediately.

Get: get File Content
For example:
  
Copy codeThe Code is as follows:
  
$ T-> set_file ("show_index", "index.htm ");
$ T-> set_file ("show_main", "main.htm ");
$ T-> set_var ("param", "boeiBlog ");
$ T-> parse ("main", "show_main ");
$ T-> parse ("index", "show_index ");
$ Getstr = $ t-> get ("index ");
Echo $ getstr; // you will see that this is actually the same as p.

With get, we can easily obtain the content of the generated page, which can be used for static page generation. We can see that phplib is very convenient for processing static pages.

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.