Smarty Advanced Application Cache operation techniques Analysis _php Example

Source: Internet
Author: User
Tags current time php template smarty template

This article describes the caching techniques for advanced applications of Smarty. Share to everyone for your reference, specific as follows:

Smarty Cache Control

Smarty provides a powerful caching capability. But sometimes we don't want the entire document to be cached, but we have the option to cache a portion of the content or a portion of the content that is not cached. For example, you use a template with a banner position at the top of the page, and the banner can contain any mix of HTML, images, flash, etc. So you can't use a static link here, and we don't want the banner to be cached. This needs to be specified in the Insert function, and a function is required to take the content information of the advertisement bar. Smarty also provides this caching control capability.

We can use {insert} to keep part of the template from being cached

You can use $smarty->register_function ($params,& $smarty) to prevent Plug-ins from outputting from the cache.

You can also use $smarty->register_block ($params,& $smarty) to keep one piece of the entire page from being cached.

Below we really to a simple requirement, explain these three kinds of methods of controlling the cache output respectively.

Requirements: The current time in the cached document is not cached and varies with each refresh.

1, use the Insert function so that part of the template is not cached

INDEX.TPL:

<div>{insert name= "Get_current_time"}</div>

index.php

function Insert_get_current_time () {return
  date ("y-m-d h:m:s");
}
$smarty =new smarty ();
$smarty->caching = true;
if (! $smarty->is_cached ()) {
  ...
}
$smarty->display (' Index.tpl ');

Annotations:

Define a function with a function name in the form of:

Inser_name (array $params, Object & $smarty),

The function argument is optional, and if additional properties are required in the Insert method of the template, they are passed to the user-defined function as an array.

Such as:

{Insert Name= ' get_current_time ' local= ' en '}

In the Get_current_time function we can get the property value by $params[' local '.

If you need to use the method or property of the current Smarty object in the Get_current_time function, you can get it by using the second argument.

At this point you will find that INDEX.TPL has been cached, but the current time is changing with each refresh.

2, using Register_function to prevent the plug-in from the cache output

INDEX.TPL:

<div>{current_time}{/div}

index.php:

function Smarty_function_current_time ($params, & $smarty) {return
  date ("y-m-d h:m:s");
}
$smarty =new smarty ();
$smarty->caching = true;
$smarty->register_function (' current_time ', ' smarty_function_current_time ', false);
if (! $smarty->is_cached ()) {
  ...
}
$smarty->display (' Index.tpl ');

Annotations:

Define a function with a function name format:smarty_type_name ($params, & $smarty)
type is a function

Name is the user's custom label name, here is {current_time}

Two parameters are required, even if they are not used in the function. Two parameters are functionally ibid.

3. Use Register_block to make a piece of the whole page not to be cached

INDEX.TPL:

<div align= ' center ' >
Page created: {"0" |date_format: "%d%h:%m:%s"}
{dynamic} now
: {"0" |date_ Format: '%d%h:%m:%s '} ... do other stuff ...
{/dynamic}
</div>

index.php:

function smarty_block_dynamic ($param, $content, & $smarty) {return
$content;
}
$smarty = new Smarty;
$smarty->caching = true;
$smarty->register_block (' Dynamic ', ' smarty_block_dynamic ', false);
if (! $smarty->is_cached ()) {
  ...
}
$smarty->display (' Index.tpl ');

Annotations:

Define a function with a function name format:smarty_type_name ($params, & $smarty)
type is block

Name is the user's custom label name, here is {dynamic}

Two parameters are required, even if they are not used in the function. Two parameters are functionally ibid.

4, summary

(1) The ability to control the cache:

Using Register_function and Register_block can easily control the buffering capacity of the output of the plug-in, you can control whether the cache by the third parameter, the default is cached, we need to display set to False, as we have done in the experiment

Copy Code code as follows:
$smarty->register_function (' current_time ', ' smarty_function_current_time ', false);

However, the Insert function is not cached by default. And this attribute cannot be modified. In this sense, the Insert function does not seem to have the ability to control caching as well as register_function and Register_block.

(2) Ease of use:

But the Insert function is easy to use. Without displaying the registration, the specified function is automatically found during the current request as long as the function is included in the current request smarty.

Of course register_function can also do not display registration at run time. But the effect of doing that is the same as other template functions, all cached, and can not be controlled.

If you use the register_function to register a custom function at run time, be sure to complete the registration of the function before calling the Is_cached () method.

Otherwise, caching the document in Is_cached () will cause smarty errors because the registration function cannot be found

Smarty user-defined function instance

<?php
$smarty->register_function (' Date_now ', ' print_current_date ');
function Print_current_date ($params, & $smarty)
{
 if (empty ($params [' format '])] {
  $format = "%b%e,%Y" ;
 } else {
  $format = $params [' format '];
 }
 Return strftime ($format, Time ());
>

Using in templates

{Date_now}
{* or to format differently *}
{Date_now format= "%y/%m/%d"}

More interested in smarty related content readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on Smarty template.

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.