Template - why and how to use them in PHP
Whether you face a website created by a programmer (may be you) and a designer for creating at the same time? Don't you know how to make your work? Let me answer: Use fastTemplate to make your site easier to customize! Ok, you may want to know why you want to use fastTemplates. · You can change your entire site in a few seconds. Abstract programming, no rubbish html code · Designers don't need to care about all "fuzzy" code · Surprisingly · It is easier to reuse old templates (for ordinary Formation) The FastTemplate originated from a Perl package with the same name (can be found on the CPAN). You can download the PHP version from its home page (this site download address is: http://www.phpe.net/downloads/1.shtml). You only need one of the classes for classes (Class.fastTemplate.php). Let me first explain what is different between the use of the template to generate a page and simply passing the page output through Echo or Print. Simply use the Echo / Print method is suitable for writing short scripts, but you can't help you better organize and customize. The template gives you the ability to create multiple language sites on the other hand, just by changing a parameter. They can prompt you to care more about what you have to do. Don't be afraid before you start the encoding. It may take some time, but these spending will have returned to you with the development of the project. So how do I apply fastTemplate? First, you need to make a simple call: Php $ tpl = new fasttemplate ("path");?> Passing a path, is a directory where you are stored in your template file. It returns an object that you can use it to assign a value, generate a page, and more. FastTemplate is based on such a hypothesis: a large page is made up of many small parts. Each section has a unique name. The smallest part is assigning a normal text string that has a unique name. This can be done by Php $ tpl-> assign (name, "text");?>. Now, if your template contains {name}, FastTemplate knows your intention. In addition, FastTemplate needs to know how you want to call your template. You need to give it a prompt by passing the associated array (Associative Array);?> To give it a prompt. PHP $ TPL-> Define (array (foo => "foo.tpl", bar => "bar.tpl"));?>> These assignments will give foo and bar to different files (name foo). TPL and Bar.tpl). Now you want FastTemplate to replace all {macros} in template foo to the corresponding value. Implemented by issuing a command PHP $ TPL-> PARSE (PageContent, "Foo"); This command will assign the contents of the template "foo" to PageContent. Of course, we haven't finished yet, because the template bar is the main page definition, fastTemplate needs to replace the {pageContent} macro.
We also need to assign a value to PageTitle, as follows: PHP $ TPL-> Assign (PageTitle, "FooBar Test"); $ TPL-> PARSE (Main, "Bar");?> Easy, isn't it? We only need to output it: PHP $ TPL-> FastPrint (main);?> The following three files show more details in the actual exercise. I don't know how to live in real life - your designer will be happy, your boss will smile because you can do more in a shorter time. Bar.tpl
php // Assign the contents of the template foo to TPL1 $ TPL-> PARSE (TPL1, "Foo"); // Attach the template bar after TPL1 $ TPL-> PARSE (TPL1, ".bar") ;?> Page.tpl