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

How to use templates in PHP_PHP Tutorial

Jul 21, 2016 pm 03:52 PM
php Why by use Can exist method template of

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...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to display child categories on archive page of parent categories How to display child categories on archive page of parent categories Apr 19, 2025 pm 11:54 PM

Do you want to know how to display child categories on the parent category archive page? When you customize a classification archive page, you may need to do this to make it more useful to your visitors. In this article, we will show you how to easily display child categories on the parent category archive page. Why do subcategories appear on parent category archive page? By displaying all child categories on the parent category archive page, you can make them less generic and more useful to visitors. For example, if you run a WordPress blog about books and have a taxonomy called "Theme", you can add sub-taxonomy such as "novel", "non-fiction" so that your readers can

Why does the Spring project cause randomness problems due to circular dependencies when starting? Why does the Spring project cause randomness problems due to circular dependencies when starting? Apr 19, 2025 pm 11:21 PM

Understand the randomness of circular dependencies in Spring project startup. When developing Spring project, you may encounter randomness caused by circular dependencies at project startup...

Why is the rise or fall of virtual currency prices? Why is the rise or fall of virtual currency prices? Why is the rise or fall of virtual currency prices? Why is the rise or fall of virtual currency prices? Apr 21, 2025 am 08:57 AM

Factors of rising virtual currency prices include: 1. Increased market demand, 2. Decreased supply, 3. Stimulated positive news, 4. Optimistic market sentiment, 5. Macroeconomic environment; Decline factors include: 1. Decreased market demand, 2. Increased supply, 3. Strike of negative news, 4. Pessimistic market sentiment, 5. Macroeconomic environment.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

Why is the return value empty when using RedisTemplate for batch query? Why is the return value empty when using RedisTemplate for batch query? Apr 19, 2025 pm 10:15 PM

Why is the return value empty when using RedisTemplate for batch query? When using RedisTemplate for batch query operations, you may encounter the returned results...

Why can't JavaScript directly obtain hardware information on the user's computer? Why can't JavaScript directly obtain hardware information on the user's computer? Apr 19, 2025 pm 08:15 PM

Discussion on the reasons why JavaScript cannot obtain user computer hardware information In daily programming, many developers will be curious about why JavaScript cannot be directly obtained...

Why does the redisTemplate.opsForList().leftPop() method not support passing in parameters to pop up multiple values ​​at once? Why does the redisTemplate.opsForList().leftPop() method not support passing in parameters to pop up multiple values ​​at once? Apr 19, 2025 pm 10:27 PM

Regarding the reason why RedisTemplate.opsForList().leftPop() does not support passing numbers. When using Redis, many developers will encounter a problem: Why redisTempl...

See all articles