How to use the template in PHP _ PHP Tutorial

Source: Internet
Author: User
How to use templates in PHP. Are you facing the challenge of creating a website created by a programmer (maybe you) and a designer at the same time? You don't know how to make work easier for both of you? Are you facing the idea of creating a website simultaneously by a programmer (maybe you) and a designer? You don't know how to make work easier for both of you? I will give you the answer: use FastTemplate to make your site easier to customize!

Well, you may want to know why you want to use FastTemplates.

· You can change the appearance of your entire site in seconds
· Abstract program design without junk HTML code
· Designers do not need to care about all the "fuzzy" code
· Surprisingly fast
· It is easier to reuse old templates (for common forms)

FastTemplate is derived from a Perl software package with the same name (which can be found on CPAN ). You can download the php version from its home page (site: http://www.phpe.net/downloads/1.shtml ). You only need a class file (class. FastTemplate. php ).

Let me explain the difference between generating a page using a template and simply outputting a page through echo or PRint.
The echo/print method is suitable for writing short scripts, but it cannot help you better organize and customize the scripts. In addition
By modifying a parameter. They can make you more concerned about what you want to do.

Do not be afraid to think before coding. It may take some time, but it will return to you as the project develops.

So, how to apply FastTemplate? First, you need to make a simple call:

?>
Pass it a path, which is the directory where all your template files are stored. It returns an object. you can use it for parameters.
Number Assignment, page generation, and so on.

FastTemplate is based on the assumption that a large page is composed of many small parts. Each part has a unique
. The smallest part is a normal text string with a unique name. You can use $ Tpl-> assign (NAME, "text ");
?>
. Now, if one of your templates contains {NAME}, FastTemplate will know you
.

In addition, FastTemplate needs to know how you want to call your template. You need to pass an associated array (associative
Array) Define ();?>
To give it a prompt.
$ Tpl-> define (array (foo => "foo. tpl ",
Bar => "bar. tpl "));
?>
These values are assigned to the foo and bar files (foo. tpl and bar. tpl) respectively ).

Now you want FastTemplate to replace all {MACROS} in the template foo with the corresponding values. By issuing commands

$ Tpl-> parse (PAGECONTENT, "foo ");
?>
. This command will assign the content of the template "foo" to PAGECONTENT. Of course, we haven't finished it yet, because the template bar is the main page definition, FastTemplate needs to replace
{PAGECONTENT} macro. We also need to assign a value to PAGETITLE as follows: $ Tpl-> assign (PAGETITLE, "FooBar test ");
$ Tpl-> parse (MAIN, "bar ");
?>
Easy, right? Now we only need to output it: $ 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 designers will be happy and your boss will smile, because you can do more things in a shorter time.

Bar. tpl

Feature world-{PAGETITLE}

{PAGETITLE}
{PAGECONTENT}


Foo. tpl
It is clear that nothing is done. See {NAME }.
Demo. php3 Include "class. FastTemplate. php3 ";

$ Tpl = new FastTemplate (".");
$ Tpl-> define (array (foo => "foo. tpl", bar => "bar. tpl "));

$ Tpl-> assign (NAME, "me ");
$ Tpl-> assign (PAGETITLE, "Welcome! ");

$ Tpl-> parse (PAGECONTENT, "foo ");
$ Tpl-> parse (MAIN, "bar ");

$ Tpl-> FastPrint (MAIN );
?>

Create the entire table
I also wrote a short example to demonstrate how to generate the entire table using a single row template. It is effective because you still do not need to direct
Modify the HTML document.

We add the content of a template to the end of a defined unique name to create an HTML table. You can call
$ Tpl-> parse (), add a "." before the template name. // Assign the content of the template foo to TPL1
$ Tpl-> parse (TPL1, "foo ");

// Add the template bar content after TPL1
$ Tpl-> parse (TPL1, ". bar ");
?>

Page. tpl


Feature world-{PAGE_TITLE}

{PAGE_TITLE}
{PAGE_CONTENT}

Table. tpl




{TABLE_ROWS}
Name Size

Table_row.tpl


{FILENAME}
{FILESIZE}

Yad. php3

Include "class. FastTemplate. php3 ";

Function InitializeTemplates (){
Global $ tpl;

$ Tpl = new FastTemplate (".");
$ Tpl-> define (array (page => "page. tpl ",
Table => "table. tpl ",
Table_row => "table_row.tpl "));
}

Function ReadCurrentDirectory (){
Global $ tpl;

$ Handle = opendir (".");
While ($ filename = readdir ($ handle )){
$ Tpl-> assign (FILENAME, $ filename );
$ Tpl-> assign (FILESIZE, filesize ($ filename ));
$ Tpl-> parse (TABLE_ROWS, ". table_row ");
}
Closedir ($ handle );
$ Tpl-> parse (PAGE_CONTENT, "table ");
}

Function PrintPage ($ title ){
Global $ tpl;

$ Tpl-> assign (PAGE_TITLE, $ title );
$ Tpl-> parse (FINAL, "page ");
$ Tpl-> FastPrint (FINAL );
}

InitializeTemplates ();
ReadCurrentDirectory ();
Printpage ("Yet Another Demo ");
?>

Speed discussion

"OK," you may say, "Everything is too good. But does it affect the speed of my website? "

No, your website may become faster. One simple reason is that, as a programmer, you are concerned with designing your applications and writing code, your code will be more efficient, it is easier and faster to process the same task. Therefore, you may add another reason for using FastTemplate in the reason list of your project as listed above.

If you only want to convert an existing web site, the success of performance may not be noticed. I suggest using regular expression buffer in PHP, which will be helpful in this case. Because FastTemplate uses regular expressions for every macro, every regular expression will be compiled only once, and the speed impact is negligible.

Maybe you and a designer create a website at the same time? You don't know how to make work easier for both of you? I come...

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.