In general, templates allow you to completely isolate your PHP code from HTML, which makes HTML graphic designers very happy and avoids losing your valuable design.
It's not fasttemplates.
So, do we really need another article on the Phpbuilder on the template? Well, yes, because there are more than one way to implement a template using PHP. Sascha's article describes how to use fasttemplates, but the PHP base Class library ("Phplib") has its own template real
Is.
What's different about them? Fasttemplates was originally transformed from a Perl library. Fasttemplates works well for Perl programs, but it's not ideal for PHP. Kristian Koehntopp the Phplib template from scratch, and as a pure PHP library, it provides a better PHP advantage. One advantage is that Kristian's design uses preg_replace () to analyze the template, which is said to be faster than the ereg_replace () used in fasttemplate. Another benefit of the phplib template is that it allows the dynamic block implementations to be nested, unlike fasttemplates.
Two libraries have very similar features and capabilities, but if you've already used 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 the phplib template does is a little bit different than fasttemplates.
Using the Phplib template
Let's start with a simple example. We assume that under/home/mydir/mytemplates/there is a template named MyTemplate, which has some text that may be:
Congratulate! You won a {Some_color}honda prelude!.
Note that "{Some_color}" is surrounded by braces. Curly braces indicate that Some_color is a template variable. We might want to write a script that can load the template, insert the value of the PHP variable $my_color in the {some_color} template variable, and then output the new text. If $my_color happens to be set to "blue", the final output may be:
Congratulate! You won a new blue Honda prelude!.
Here is the result of the PHP script:
<?php
include "template.inc";
$my_color = "blue";
// 将在后面使用
$t = new Template("/home/mydir/mytemplates/");
// 创建一个名为 $t 的模板对象
$t->set_file("MyFileHandle","MyTemplate.ihtml");
// 设置 MyFileHandle = 我们的模板文件
$t->set_var("some_color",$my_color);
// 设置模板变量 some_color = $my_color值
$t->parse("MyOutput","MyFileHandle");
// 设置模板变量 MyOutput = 分析后的文件
$t->p("MyOutput");
// 输出 MyOutput 的值(我们的分析后的数据)
?>
The first line is an include directive, which is used to provide phplib template functionality. Of course phplib do more than templates, but if you only want to use template features, you only need to include tmplate.inc (Template.inc is one of the files from Phplib). The Phplib template uses object-oriented programming, so the next thing is to create a template object. Code <?php $t = new Template (
"/home/mydir/mytemplates/");?>
Create a new Template object $t. This $t object is a handle that will be used to process all of the template functions for other code in the PHP script. If you want, you may create other template objects (each with its own template variable namespace), but one will suffice. The path ("/home/mydir/mytemplates/") in the template's constructor call is used to set the root of your template's location, but if you do not set it, it defaults to the same directory as your PHP script.
Then we call Set_file () to define a handle named "Myfilehandle" to be linked to the mytemplate.ihtml (the template is not actually loaded until parse () is invoked). By the way, the phplib template filename has a suffix of. Ihtml is a habit that you can use. Html,.tpl, or other suffixes. Then call Set_var () to set the value of the template variable Some_color to $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 loads the Myfilehandle (mytemplate.ihtml) for analysis and replaces all template variables ("{A variable}") as the value of the template variable, and the result of the analysis is placed in the myoutput. No results are output to the Web server unless P ("Myoutput") is invoked, it outputs the last parsed text.