Ways to use templates in PHP _php templates

Source: Internet
Author: User
Tags regular expression
Well, you might want to know why you're using fasttemplates.

• Can change the appearance of your entire site in a few seconds
• Abstract programming, no spam HTML code
• Designers do not need to care about all of the "fuzzy" code
• Surprisingly fast
• Easier reuse of old templates (for plain forms)

Fasttemplate originates from a Perl software package with the same name (which can be found on CPAN). You can download the version of PHP from its homepage (download the address for this site: http://www.phpe.net/downloads/1.shtml). You just need one of the class's Files (class. fasttemplate.php).



Let me first explain the difference between using a template to generate a page and simply outputting the page output through Echo or print.
Simply using Echo/print is a good way to write short scripts, but not to help you organize and customize them better. Template on the other hand gives
Your ability to create a multi-language site, just by changing one parameter. They can motivate you to be more concerned about what you want to do.



Don't be afraid to think before you start coding. It may take some time, but these costs will pay off as the project progresses.



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

<?php $tpl =new fasttemplate ("path");?>

Pass it a path that is the directory where all your template files are stored. It returns an object that you can use to participate in
Number assignments, generating pages, and so on.

Fasttemplate is based on the assumption that a large page is made up of a lot of small parts. Each part has a unique
's name. The smallest part is an assignment to a normal text string with a unique name. This can be done by
<?php
$tpl->assign (NAME, "text");
?>
To complete. Now, if you have a template that contains {name},fasttemplate, you
's intentions.


In addition, fasttemplate need to know how you want to call your template. You need to pass an associated array (associative
Array) to <?php $tpl->define ();?>
To give it a hint.
The following are the referenced contents:
<?php
$TPL->define Array (foo => "Foo.tpl",
Bar => "Bar.tpl"));
?>

These assignments will give Foo and bar different files (named Foo.tpl and BAR.TPL) respectively.

Now you want Fasttemplate to replace all {MACROS} in template foo with the corresponding values. By issuing an order

The following are the referenced contents:
<?php
$tpl->parse (pagecontent, "foo");
?>

To achieve.   This command assigns the contents of the template "foo" to PageContent. Of course, we're not done yet, because the main page definition in template bar, Fasttemplate needs to replace the
{PAGECONTENT} macro. We also need to assign values to PageTitle as follows:
The following are the referenced contents:
<?php
$tpl->assign (pagetitle, "FooBar test");
$tpl->parse (MAIN, "Bar");
?>

It's easy, isn't it? Now we just need to output it: <?php
$tpl->fastprint (MAIN);
?>
The following three files show more detailed descriptions in the actual practice. I don't know how to live in the real world without the technology
Your designer will be happy and your boss will smile because you can do more in a shorter time.



The following are the referenced contents:
Bar.tpl
<!--BAR.TPL-->
<HTML>
<body Bgcolor=black text=white>
<H1>{PAGETITLE}</H1>
{PageContent}
</BODY>
</HTML>
Foo.tpl <!--foo.tpl-->

It was obvious that nothing had been done. Please see {NAME}.
The following are the referenced contents:



Demo.php3
<?php
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 an entire table
I also wrote a short example that shows how to generate an entire table from a single line template. It works because you still don't need to be straight
Modify the HTML document.



We add the contents of a template to an already defined unique name behind to create an HTML table. This can be done by calling the
$tpl->parse (), add a "." Before the template name. To achieve. <?php
Assign the contents of the template foo to TPL1
$tpl->parse (TPL1, "foo");

Attach the contents of the template bar after TPL1
$tpl->parse (TPL1, ". Bar");
?>

Page.tpl




The following are the referenced contents:
<HTML>
<body Bgcolor=black text=white>
<H1>{PAGE_TITLE}</H1>
{Page_content}
</BODY>
</HTML>
Table.tpl



The following are the referenced contents:
<TABLE>
<TR> <TH>name</TH> <TH>size</TH> </TR>
{Table_rows}
</TABLE>





Table_row.tpl

The following are the referenced contents:
<TR>
<TD>{FILENAME}</TD>
<TD>{FILESIZE}</TD>
</TR>




Yad.php3



The following are the referenced contents:
<?php
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 might say, "It's all very well." But does it not affect the speed of my website? "Www~

No, your site may become faster. A simple reason: because you are concerned with designing your application and writing code as a programmer, your code will be more efficient and the same tasks easier and quicker to handle. So you may add another reason to the list of reasons why you might consider using Fasttemplate in your project.

If you just want to convert an existing Web site, performance success may not be noticed. I recommend using regular expression buffering in PHP, which will help in this case. Because Fasttemplate uses regular expressions for each macro, each regular expression is compiled only once and the effect of the speed can be negligible.

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.