Home > Backend Development > PHP Tutorial > How to use templates in PHP_PHP Tutorial

How to use templates in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:52:01
Original
911 people have browsed it

Okay, you might be wondering why you should use FastTemplates.

·Can change the look of your entire site in seconds
·Abstract programming, no junk HTML code
·Designers don't need to care about all the "obscure" code
· Surprisingly fast
·Easier to reuse old templates (for regular forms)

FastTemplate is derived from a Perl package with the same name (can be found on CPAN). You can download the PHP version from its homepage (the download address of this site is: http://www.phpe.net/downloads/1.shtml). You only need one of the class files (class.FastTemplate.php).



Let me first explain the difference between using a template to generate a page and simply outputting the page through echo or print.
Simply using the echo/print method is great for writing short scripts, but it doesn’t help you organize and customize it better. Templates on the other hand give you
the ability to create multilingual sites just by changing a parameter. They can push you to care more about what you have to do.



Don’t be afraid to think before you start coding. It may take some time, but the cost will pay off as the project grows.



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



Pass it a path, which is all your The directory where template files are stored. It returns an object, which you can use to assign parameters, generate pages, etc.

FastTemplate is based on the assumption that a large page is composed of many small parts. Each part has a unique
name. The smallest part is assigning it to a normal text string with a unique name. This can be done via
$tpl->assign(NAME, "text");
?>
. Now, if one of your templates contains {NAME}, FastTemplate knows what you
meant.


In addition, FastTemplate needs to know how you want to call your template. You need to give it a hint by passing an associative
array to define(); ?>
.
The following is the quoted content:
$tpl->define(array(foo => "foo.tpl",
bar => "bar.tpl" ));
?>

These assignments will give foo and bar respectively different files (named foo.tpl and bar.tpl).

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

The following is the quoted content:
$tpl->parse(PAGECONTENT, "foo");
?>

to achieve. This command will assign the contents of template "foo" to PAGECONTENT. Of course, we are not done yet, because the template bar is the main page definition, and FastTemplate needs to replace the
{PAGECONTENT} macro. We also need to assign a value to PAGETITLE, as follows:
The following is the quoted content:
$tpl->assign(PAGETITLE, "FooBar test");
$tpl ->parse(MAIN, "bar");
?>

Easy, isn't it? We just need to output it now: $tpl->FastPrint(MAIN);
?>
The following three files show more detailed descriptions of the actual exercise. I don't know how I would live without this technology in real life --
Your designers will be happy and your boss will smile because you can do more in less time.



The following is the quoted content:
bar.tpl


< ;HEAD>Feature world - {PAGETITLE}

{PAGETITLE}{PAGECONTENT}


foo.tpl

Clearly displayed Nothing was done.Please see {NAME}.
The following is the quoted content:



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 through a single-row template. It works because you still don't need to modify the HTML document directly.



We create an HTML table by adding the content of a template to an already defined unique name. This can be achieved by adding a "." before the template name when calling
$tpl->parse(). // Assign the content of template foo to TPL1
$tpl->parse(TPL1, "foo");

// Attach the content of template bar after TPL1 Content
$tpl->parse(TPL1, ".bar");
?>

page.tpl




below For quoted content:

Feature world - {PAGE_TITLE}

{PAGE_TITLE}


{PAGE_CONTENT}
>

The following is the quoted content:


{TABLE_ROWS}
name size






table_row.tpl

The following is the quoted content:

{FILENAME}
{FILESIZE}
>

yad.php3



The following is the quoted content:
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, "everything is great. But won't it affect the speed of my site? " www~

No, your website will probably get faster. One simple reason: because you as a programmer are concerned with designing your application and writing code, your code will be faster Efficiency, it's easier and faster to handle the same tasks. So, you might add another reason to the list of reasons listed above why you should consider using FastTemplate in your project.

If you just want to. Converting an existing web site, the performance success may not be noticed. I recommend using regex buffering in PHP, which will help in this case because FastTemplate uses regex for every macro. formula, each regular expression will be compiled only once and the speed impact is negligible

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319003.htmlTechArticleOkay, you might be wondering why you should use FastTemplates. ·Can change the look of your entire site in seconds ·Abstract programming, no junk HTML code ·Designers don’t need to...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template