Home > Backend Development > PHP Tutorial > Template, PHPLIB processing method 1_PHP tutorial

Template, PHPLIB processing method 1_PHP tutorial

WBOY
Release: 2016-07-13 17:24:54
Original
816 people have browsed it

If you're wondering what templates are, start by reading the first few paragraphs of the excellent article "Templates - why and how to use them in PHP3" by Sascha Schumann.
Generally speaking, templates allow you to completely separate your PHP code from HTML, which makes HTML graphic designers very happy and prevents them from losing your valuable design.
It’s not FastTemplates
So, do we really need another article about templates on PHPBuilder? Well, yes, because there is more than one way to implement templates in PHP. Sascha's article describes how to use FastTEmplates, but the PHP Basic Library ("PHPLIB") has its own template implementation.
What’s the difference between them? FastTemplates was originally converted from a Perl library. FastTemplates work well for Perl programs, but are less than ideal for PHP. Kristian Koehntopp wrote the PHPLIB template from scratch as a pure PHP library, which better provides the advantages of PHP. One benefit is that Kristian's design uses preg_replace() to analyze templates, which is said to be faster than the ereg_replace() used in FastTemplate. Another benefit of PHPLIB templates is that they allow dynamic block nesting, unlike FastTemplates.
Both libraries have very similar features and capabilities, but if you already use FastTemplates, and you want to learn to use PHPLIB templates, you should forget everything you know about FastTemplates. Their features may be similar, but everything PHPLIB templates do is just a little bit different than FastTemplates.
Using PHPLIB Templates
Let’s start with a simple example. Let's assume that there is a template called MyTemplate under /home/mydir/mytemplates/, which has some text, and the content may be:
Congratulations! You won a {some_color}Honda Prelude!
Note that "{some_color}" is surrounded by curly brackets. The curly braces indicate that some_color is a template variable. We might want to write a script that loads the template, inserts the value of the PHP variable $my_color in place of the {some_color} template variable, and then outputs the new text.


If $my_color happens to be set to "blue", the final output might be:
Congratulations! You win a new blue Honda Prelude!
The following is the PHP script for the above results:
------------------------------------------------ -----------------------------------------------
include "template.inc";
$my_color = "blue";
// will be used later
$t = new Template("/home/mydir/mytemplates/");
// Create a template object named $t
$t->set_file("MyFileHandle", "MyTemplate.ihtml");
// Set MyFileHandle = our template file
$t ->set_var("some_color",$my_color);
//Set template variable some_color = $my_color value
$t->parse("MyOutput","MyFileHandle");
//Set template Variable MyOutput = analyzed file
$t->p("MyOutput");
// Output the value of MyOutput (our analyzed data)
?>------- -------------------------------------------------- -----------------------
The first line is an include directive, which is used to provide PHPLIB template functionality. Of course PHPLIB does more than templates, but if you just want to use template features, just include tmplate.inc (template.inc is one of the files from PHPLIB). PHPLIB templates use object-oriented programming, so the next thing to do is to create a template object. Code $t = new Template ("/home/mydir/mytemplates/" ); ?> creates a new template object $t. This $t object is a handle that will be used to handle all template functions and other code in the PHP script. If you wish, you may create other template objects (each with its own template variable namespace), but one will suffice. The path in the template's constructor call ("/home/mydir/mytemplates/") is used to set the root directory where your templates are located, but if you don't set it, it will default to the directory where your PHP scripts are located. Same.
Then we call set_file() to define a handle named "MyFileHandle" to link with MyTemplate.ihtml (the template will not actually be loaded until parse() is called). By the way, it is a convention that the suffix of the template file name of the PHPLIB template is .ihtml. You can use .html, .tpl, or other suffixes. Then call set_var() to set the template variable some_color to the value of $my_color (the value is "blue"), meaning that all occurrences of {some_color} in the template will be replaced by the word "blue" once we call parse().
Then we call parse(), which will load MyFileHandle (MyTemplate.ihtml) for analysis, and replace all template variables ("{a variable}") with the value of the template variable, and the analysis results will be placed in MyOutput. No results will be output to the web server unless p("MyOutput") is called, which will output the last parsed text.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532094.htmlTechArticleIf you are wondering what a template is, first take a look at the wonderful article "Templates-Why and How to use them in PHP3 (Templates - why and how to use them in...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template