Template, Phplib processing method (i)

Source: Internet
Author: User
Templates If you are wondering what a template is, first take a look at the previous paragraphs of Sascha Schumann's wonderful article, "templates-why and how they can be used in PHP3 (templates-why and the use of them in PHP3)."





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


now.





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 all 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.





Use 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:





Congratulations! 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:





Congratulations! You won a new blue Honda prelude!.





The following is the result of the PHP script: <?php


include "Template.inc";





$my _color = "Blue";


//will be used in the back





$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 = parsed file





$t->p ("Myoutput");


//Output Myoutput value (after our analysis of the data)


?>


The first line is an include instruction that provides 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/");?>


creates 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 link to the mytemplate.ihtml (the template is not actually loaded until the 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.








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.