Examples of learning PHP fasttemplate template Chapter

Source: Internet
Author: User
Tags define array end header include php language variable win32
Templates If you've never been in touch with PHP, take a look at this, of course, even though you already know something about PHP, a PHP4 manual is needed, too:. In addition, an HTML grammar manual is also indispensable .....

During the development of the website, do you often face the troubles of the revision? Hundreds of thousands of files because the layout of a small change in the need for all of the new treatment, is not to give you a headache? Alas, it would be nice to separate the content from the manifestation, which we have been waiting for. But unfortunately the XML used to deal with this problem is not yet fully mature. Is there no way beyond that? The so-called things are dead, people are alive, we want to learn this PHP library today, can help us to some extent to deal with this problem. :))

What is Fasttemplate? From the PHP language it is a PHP library; From its origins it derives from a Perl package of the same name; it is a utility that allows you to change the appearance of the entire site in seconds. In the most popular language, it is a template, a template similar to Dreamwaver. Now fasttemplate in your heart is a question mark? Or an exclamation point? Or a period? (edit: Rely on, in this cheat royalties ah, I flat!) Forget it, no matter how much, you just know he is a good thing,:

First of all before we use this library, of course, first download it (edit: ' Nonsense, and cheat royalties ah '. Hide it: ' Well, well, I won't say it. :( '。 , you can download it http://www.thewebmasters.net/php/the URL below. After downloading it, unzip it to a directory on your Web server, and the following is the unpacked directory structure

fasttemplate-1.1.0/
Fasttemplate-1.1.0/readme <-This file is needless to say, right?
Fasttemplate-1.1.0/class. Fasttemplate.php3 <-This file is the most important, it's on the library file, the core Yes!
Fasttemplate-1.1.0/example_1.phtml <-an example
Fasttemplate-1.1.0/example_2.phtml <-second example
Fasttemplate-1.1.0/example_3.phtml <-A third example
Fasttemplate-1.1.0/dynamic_example.phtml <-fourth example

fasttemplate-1.1.0/docs/<-Documents 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/<-Directory of template examples
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 that the PHP program can access, in other words, the Include directory in the php.ini. And then, using PHP4 programming friends notice, you can not use this library directly, but also need to manually make some changes! Wait and see below. PHP3 readers don't care so much, now you can try it with a few examples, (hey, but, it's those examples of the suffix name is all phtml, if you can't set the Web. You can try to change the suffix to see, you should also be able to use, to hide did not dare to pack a ticket. That Then, PhP4 Brothers can be sure to pay attention to yo, if you do not press the following changes, this library but no way to use YO!!

---class. FASTTEMPLATE.PHP3 Sun June 27 13:44:47 1999
+++ PHP4. FASTTEMPLATE.PHP3 Tue 20 10:49:25 1999
@@ -196,8 +196,10 @@
Settype ($val, "string");
}

-$template = Ereg_replace ("{$key}", "$val", "$template");
-//$template = Str_replace ("{$key}", "$val", "$template");
+///PHP4 doesn ' t like ' {$ ' combinations.
+ $key = ' {'. ' $key ". '} ';
+ $template = ereg_replace ("$key", "$val", "$template");
+//$template = Str_replace ("$key", "$val", "$template");
}
}

@@ -410,7 +412,7 @@
}
if ($end)
{
-$newParent. = "{$MacroName}
";
+ $newParent. = ' {'. ' $MacroName}
";
}
Next Line
if ($end) {$end = false;}

You open class with a text editor. FASTTEMPLATE.PHP3 file, find the above section. '-' The minus sign represents the elimination of this line, ' + ' plus sign to join this line. In addition windows system under the attention of friends yo! You also need to a pity Dorado small things, the following the value of the $WIN32 variable to true, do not change, you can not scold me,;

var $WIN = true; Set to True if it is a WIN32 server

After the change, we can try his examples of these. How is it? It's a success. Hey Hey Hei ~ ~ ~, the hidden will not deceive everyone.

OK, now this library is ready to use, we are prepared to do the following study,:

How do I use this library? Simple, first of all, to include this library. In other words, to include "class." Fasttemplate.php3 "; And then? Oh...... Then I don't know! (Editor: "Hey!") Look for a flat, you! "Ah, ah ...! Sorry, I think of it (after the Tibetan people edited the sea flat after a meal). Then learn how to use the library. Here, let's make a hypothesis.

Let's assume that a page is made up of a lot of small parts (for example: Every site has a column navigation, and so on), and each small part has a unique identifier (for example: we define each column in the compartment as a name)

We first take the example_1 of this library as an example, so that we can learn the basic use of this library as soon as possible, but we should pay attention yo, these several files on the server location to press the relative path of decompression to save, don't forget, like don't forget your girlfriend's birthday. hehe ~ ~ ~ ~ ~

<?

Example fasttemplate Demo #1-the Example from the man page

Header ("Content-type:text/plain");

Include ("Class.") Fasttemplate.php3 ");
$TPL = new Fasttemplate ("./templates");

$TPL->define (
Array
main = > "Main.tpl",
Table = > "Table.tpl",
row = > "Row.tpl"
)
);

$TPL->assign (Array (TITLE = > "Fasttemplate Test"));

for ($n =1; $n < = 3; $n + +)
{
$Number = $n;
$BigNum = $n *10;
$TPL->assign (
Array
Number = > $Number,
Big_number = > $BigNum
)
);
$TPL->parse (ROWS, ". Row");
}

$TPL->parse (Main, Array ("table", "main"));

$TPL->fastprint ();

Exit

? >

<!--NAME:MAIN.TPL-->
< HTML >
< head >< title > {title} </title >
< BODY >
{MAIN}
</body >
<!--END:MAIN.TPL-->

<!--NAME:TABLE.TPL-->
< table border= ' 1 ' >
{ROWS}
</table >
<!--END:TABLE.TPL-->

<!--NAME:ROW.TPL-->
< tr >
< TD >{NUMBER}</td >
< TD >{BIG_NUMBER}</td >
</tr >
<!--END:ROW.TPL-->

Use this library first as described in the include "class" above. Fasttemplate.php3 "; Then it is the directory where the template is defined $TPL = new Fasttemplate ("./templates"); Note yo! Here because I am running in the Windows system, so I use "./templates", in Linux or Unix may not be the same, you set according to the specific situation. Then, we correspond to different templates and define the matrix array below. $TPL->define (Array (main = > "Main.tpl", table = > "Table.tpl", row = > "Row.tpl")); Define is a function in this class. Here's its syntax: define (Array (Key,value pairs)), define () function maps a name to a template file, and the new name will be the only name you use to represent the template, because there will be no more template filenames. And we note that this is the use of the class must not be missing steps, not less yo, if less then and you do not bring birthday gifts to your girlfriend's birthday party has the same thing, hey Hey Hei ~ ~ ~ ~

Right now! The key, the most personalized thing comes! Assign (Key,value pair) or assign (array (key value pairs) This function defines the identifiers you define in the template as what you really want on your Web page. Like the top $tpl->assign (The TITLE = > "Fasttemplate Test")); In this sentence, replace the {TITLE} in the template main.tpl with the fasttemplate Test to see the above code more than the unknown friend. What's next! Then there is another very special thing. Parse (return, FileHandle (s)) inserts a defined template into a different template file. The following code is carefully studied, which is believed to be more easily understood.

for ($n =1; $n < = 3; $n + +)
{
$Number = $n;
$BigNum = $n *10;
$TPL->assign (
Array
Number = > $Number,
Big_number = > $BigNum
)
);
$TPL->parse (ROWS, ". Row");
}

Parse this function has three different uses

$TPL->parse (Main, "main"); Standard
$TPL->parse (Main, Array ("table", "main")); Simple
$TPL->parse (MAIN, ". Row"); Increase

One of the most interesting is the third, which represents the addition of a new data on the basis of the original. Oh, so we may not understand, we still see more research on the above source code, after all, programming this thing has a lot is sensed inexpressible! (Enclosed in English instructions, not to hide to lazy do not translate, there are some things to see the original flavor than the translation of easier to understand Ah,

In the regular version, the template named ' main ' is loaded if it hasn ' t been already, the variables are D, and the result is then stored in fasttemplate as the value MAIN. If the variable ' {main} ' shows up in a later template, it'll be interpolated to be the value of the ' parsed ' ' main ' ' Temp Late. This allows your 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 as:

$TPL->parse (Main, Array ("table", "main"));

This form saves function calls and makes your code cleaner

It is important to are using the compound form, which template after the "the", must contain the VARIABL E That's are parsing the results into. In the above example, ' main ' must contain the variable ' {main} ', as this is where the parsed results of ' table ' is stored. If ' main ' does not contain the variable ' {main} ' then the parsed of ' table ' would be results. 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 a dynamic number of Rows-such as data from a database query. )

Finally, when the above process is completed, the output is available, and the Fasttemplate Library uses Fastprint (HANDLE) for output.

$TPL->fastprint ();

After these steps, is the process of using the Fasttemplate library clearly understood? Try to summarize the above process and see if it will help your friends:

First: Add the library with include

Second: Define a class variable, in the example above it is $TPL

Again: Define a variety of minimum elements, such as the $TPL = new Fasttemplate ("./templates");

Then: Use the parse function to combine all the smallest elements

Finally: Using Fastprint () for output

This is the simplest procedure to use fasttemplate. The hidden writing is not good, also do not know how many people can learn from it, ":-)

Related Article

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.