Examples of PHP FastTemplate templates if you have never been in touch with PHP, let's take a look at this first. of course, even if you already know something about PHP, but a PHP4 user manual is still needed. :) If you have never been in touch with PHP, let's take a look at it first. of course, even if you already know something about PHP, but a PHP4 User Manual still needs to be written ,:). In addition, an HTML syntax manual is of course indispensable ..........
Do you often suffer from revision changes during Website development? A few hundred or thousands of files need to be completely re-processed due to a small change in the layout. Is it a headache for you? Alas, it would be nice to separate the content from the form of expression, but we have been waiting for it. But unfortunately, the XML used to handle this problem is not yet completely mature. Is there any other way out of this? The so-called things are dead, but people are living. The php library we are going to learn today can help us solve this problem to a certain extent. :))
What is FastTemplate? In terms of PHP language, it is a PHP library; in terms of its origins, it originates from a Perl software package with the same name; in terms of usage, it is a utility that allows you to change the appearance of the entire site within a few seconds. In the most popular language, it is a template, similar to a template in DreamWaver. Now FastTemplate is a question mark in your mind? Or an exclamation point? Or a full stop? (Edit: depend on it. here, I am lying about the draft fee !) Forget it. no matter how much it is, you only need to know that it is a good thing ,:)
Before using this library, you must download it first (edit: 'Nonsense, cheat the article fee '. Location: 'Okay, okay, I won't talk about it anymore. :('.), You can download this url at http://www.thewebmasters.net/php. After the download, decompress the package to a directory on your web server. the directory structure is as follows:
FastTemplate-1.1.0/
FastTemplate-1.1.0/README <-don't say this file?
FastTemplate-1.1.0/class. FastTemplate. php3 <-This file is the most important, it is the library file, the core!
FastTemplate-1.1.0/example_1.phtml <-an example
FastTemplate-1.1.0/example_2.phtml <-Example 2
FastTemplate-1.1.0/example_3.phtml <-Third example
FastTemplate-1.1.0/dynamic_example.phtml <-fourth example
FastTemplate-1.1.0/docs/<-document directory
FastTemplate-1.1.0/docs/FastTemplate.3 <-Unix man page
FastTemplate-1.1.0/docs/FastTemplate.html <-HTML documentation
FastTemplate-1.1.0/templates/<-template example Directory
FastTemplate-1.1.0/templates/begin. tpl
FastTemplate-1.1.0/templates/header. tpl
FastTemplate-1.1.0/templates/main. tpl
FastTemplate-1.1.0/templates/row. tpl
FastTemplate-1.1.0/templates/test. tpl
FastTemplate-1.1.0/templates/footer. tpl
FastTemplate-1.1.0/templates/htaccess. tpl
FastTemplate-1.1.0/templates/middle. tpl
FastTemplate-1.1.0, templates, table. tpl
Note that this directory must be a directory accessible to the php program. In other words, it is the include directory in php. ini. Then, the PHP 4 programming friends noticed that you cannot directly use this library now, and you still need to manually modify it! And so on. Users of php3 don't need to worry so much about it. now we can try the examples it carries. (Hey, but the extensions of those examples are all phtml, if you cannot set the web. You can try to change the suffix and use it. you have never tried to pack a ticket .) That! Next, you must pay attention to the php4 brothers. if you do not modify the database as follows, there is no way to use this database !!
@-Random, 7 + 412,7 @@
}
If ($ end)
{
-$ NewParent. = "{$ MacroName}
";
+ $ NewParent. = '{'. "$ MacroName}
";
}
// Next line please
If ($ end) {$ end = false ;}
Open the class. FastTemplate. php3 file in the text editor and find the above section. '-' Minus signs indicates that this line is eliminated, and '+' indicates that this line is added. In addition, if you are in windows, pay attention to it! You also need to change the value of the $ win32 variable below to true. if you don't change the value, don't scold me if you don't ,;).
Var $ WIN32 = true; // Set to true if this is a WIN32 server
After modification, you can try his examples. How? Succeeded. Hey, hey, hey ~~~, It won't lie to everyone.
OK. Now this database is ready for use. let's prepare for the following learning ,:)
How to use this library? Simple: first, you need to include this library. That is to say, we need to include "class. FastTemplate. php3"; then? Haha ...... And I don't know! (Edit: "Hey! Looking for something, you! ") Ah, ah ...! Sorry, I remember (after being edited by the crowd ). Then I learned how to use this library. Here, let's make a hypothesis first.
Let's assume that a page is composed of many small parts (for example, each website has a navigation bar), and each small part has a unique identifier (for example: we define each item in the column as a name)
Let's take example_1, which comes with this library as an example, so that you can learn the basic usage of this library as soon as possible;), but you should pay attention to it, these files should be stored on the server according to the relative path during decompression. never forget, just like never forget your girlfriend's birthday. Haha ~~~~
<?
// Example FastTemplate Demo #1-The example from the man page
Header ("Content-type: text/plain ");
Include ("class. FastTemplate. php3 ");
$ Tpl = new FastTemplate ("./templates ");
Before using this library, you must first include "class. FastTemplate. php3", and then define the directory where the template is located $ tpl = new FastTemplate ("./templates"). be careful! Here, because I am running on windows, I use "./templates". it may be different in Linux or UNIX. you can set it according to your actual situation. Then, we define the matrix array below for different templates. $ Tpl-> define (array (main => "main. tpl ", table =>" table. tpl ", row =>" row. tpl "); define is a function in this class. The following is its syntax: define (array (key, value pairs), define () function maps a name to the template file, the new name will be the unique name you use to represent the template, because there will be no template file name in addition. In addition, you must note that this is an indispensable step in the use of the category. if it is missing, it will be the same as if you don't bring a birthday present to your girlfriend's birthday Party, ~~~~
Now! The key is that the most personalized things are coming! The assign (key, value pair) or assign (array (key value pairs) function defines the identifier you define in the template as what you really want on your webpage. For example, in the above $ tpl-> assign (array (TITLE => "FastTemplate Test. replace {TITLE} in tpl with FastTemplate Test. if you are not clear about it, read the above code. Next! Then there is another special stuff. Parse (RETURN, FileHandle (s) inserts a defined template into another template file. The following code is easy to understand.
For ($ n = 1; $ n <= 3; $ n ++)
{
$ Number = $ n;
$ BigNum = $ n * 10;
$ Tpl-> assign (
Array (
NUMBER => $ Number,
BIG_NUMBER => $ BigNum
)
);
$ Tpl-> parse (ROWS, ". row ");
}
The third is the most interesting. it indicates that a new data is added based on the existing data. Well, you may not be able to understand this. let's take a look at the source code above. after all, there are a lot of programming things that can't be said! (The English description is attached below. it is not intended to be lazy or not translated by Tibetan. it is actually easier to understand some things than the original translation,
In the regular version, the template named ''main'' is loaded if it hasn' t been already, all the variables are interpolated, and the result is then stored in FastTemplate as the value MAIN. if the variable '{MAIN}' shows up in a later template, it will be interpolated to be the value of the parsed ''main'' template. this allows you to easily nest templates, which brings us to the compound style.
The compound style is designed to make it easier to nest templates.
The following are equivalent:
$ Tpl-> parse (MAIN, "table ");
$ Tpl-> parse (MAIN, ". main ");
// Is the same:
$ Tpl-> parse (MAIN, array ("table", "main "));
// This form saves function CILS and makes your code cleaner
It is important to note that when you are using the compound form, each template after the first, must contain the variable that you are parsing the results. in the above example, 'main' must contain the variable '{main}', as that is where the parsed results of 'table' is stored. if 'main' does not contain the variable' {main} 'then the parsed results of 'table' will be lost. the append style allows you to append the parsed results to the target variable. placing a leading dot. before a defined file handle tells FastTemplate to append the parsed results of this template to the returned results. this is most useful when building tables that have an dynamic number of rows-such as data from a database query .)
Finally, you can output the above process. The FastTemplate library uses FastPrint (HANDLE) for output.
$ Tpl-> FastPrint ();
After the above steps, do you understand the process of using the FastTemplate library? Try to summarize the above process to see if it is helpful to your friends:
First, add the library with include.
Second, define a class variable. in the above example, it is $ tpl.
Again: define various minimum elements, such as the above $ tpl = new FastTemplate ("./templates ");
Then the parse function is used to combine various minimum elements.
Finally, use FastPrint () for output.
The above is the simplest process to use FastTemplate. I don't know how much you can learn from the poor writing of Tibetan data ,《:-)
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.