PHP Smarty Review

Source: Internet
Author: User
Tags smarty template

Smarty: Template Technology

Realization function: Separation before and after.

Principle: Mainly through the Smarty core class implementation, call the display method, the template file read, replace with regular, replace save to temporary file, the temporary file loaded into the current page.

Configuration file (Portal file): Instantiate the Smarty class and set the file save path

If you use the Smarty template on the page, you need to introduce the configuration file to the current page.

The page that the browser wants to access is a background page (. php), and the page that is eventually seen in the browser is the template page (. html or. TPL)

The file path inside the template: The relative path based on the. php file, or the absolute path if you do not use a relative path.

The template shows the variables:

1. Variables to be written between smarty tags, variable names starting with $

<{$test}>

2. Supports all classes of data, including arrays (associative arrays), objects

Associative arrays in the template display, in addition to the use of PHP syntax, but also support point syntax <{$attr .name}>

Read the configuration file inside the template

1. Load the configuration file to the current page
<{config_load file= ' test.conf '}>

2. Read the contents of the configuration
<{#color #}>

3. If a block is divided in the configuration file, how to read the configuration of a piece
<{config_load file= ' test.conf ' section= ' block name '}>

Smarty Reserved variables

$smarty. Const read constants such as: Css,js, Image storage junction

Variable adjuster:

Keyword: modifier

1. System comes with variable adjuster: Turn capital to lowercase first uppercase intercept string stitching string
2. Custom variable adjuster:

Variable adjuster Storage directory new file: modifier. Regulator name. php
Create a new method in the file: function smarty_modifier_ adjuster name ($STR) {}
$str parameter represents the variable that invokes the regulator
Finally, the method must have a return value

Function:

Keywords: function common functions, block blocks function

1. The system comes with functions: if foreach

<{if condition}>
Satisfying conditional output content
<{else}>
Do not meet conditional output content
<{/if}>

<{if condition 1}>
Satisfying conditional output content
<{elseif condition 2}>
Does not meet the condition 1 satisfies the condition 2 executes
<{/if}>

When comparing, there are alternate words that can be used

<{foreach $shuzu as $v}>

[email protected] Index starting at 0
[email protected] represents the first loop
[email protected] represents the last loop

<{/foreach}>

2. Custom Functions

Locate the folder where the function plug-in is located create a new file: function. Name. PHP (block. function name. php)
Create a new method in the file: function Smarty_function_ name ($args) {}
function Smarty_block_ name ($args, $nr, $smarty, $bs) {}

Parameter $args: Call the function to pass in the property association parameter
The contents of the clip between the parameters $nr:block block
Parameter $smarty: Object
Parameter $bs: Whether it is the first call (inside the start tag)

The method eventually has a return value

Template inheritance:

Extends block

Parent Template: The head and tail used to put a Web page
You need to reserve a certain area for the sub-page modification to replace
Use <{block name= ' Neirong '}><{/block}>
In general, the parent template has at least two areas reserved, one of which is inside the head and the other inside the body.

Sub-page: General inheritance of the parent template to put this page implementation of the function
<{extends file= '}>
Refine the areas reserved in the replacement parent template:
<{block name= ' Neirong '}><{block}>


Cache:

Practice:
1. Define a path where the cache file is stored
2. Determine if the cache file exists or is out of date
3. If the file does not exist or expires:
Open Memory Cache Ob_start ();
Take the contents of the Memory ob_get_contents ();
Store content in a cache file file_put_contents ();
Close Memory cache Ob_flush ();
4. If the file exists or is not expired:
Introduce the cache file to the current page include ();

PHP Smarty Review

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.