Simple Application of PHP smarty templates

Source: Internet
Author: User
Tags pconnect

What is a template? My understanding is to separate the interface from the implementation code,

This aims to separate the work of the artist and the programmer, improve work efficiency, and facilitate later maintenance.

PHP also has many templates, such as phplib template, fasttemplate, and smarty.

Today I am studying smarty

Download the latest smarty kit: http://smarty.php.net/
After unlocking smarty 2.6.0, you will see many files, including a libs folder. In libs, there should be 3 class. php labels + 1 Debug. TPL + 1 plugin folder + 1 core folder. Then copy libs directly to the main folder of your program

The libs in the above project structure is the smart internal library

Common. phpEnhance repeatability. This is because many pages use the following settings, so extract them. You only need to reference them each time!

<? PHP
Require ('smarty/MySQL. Class. php ');
$ Dbcharset = 'utf8 ';
$ Query = new dbquery ('localhost', 'root', 'sa', 'Demo ');

Require 'smarty/libs/smarty. Class. php ';

// Initialize smarty
$ Smarty = new smarty;

$ Smarty-> compile_check = true; // open the compilation check.
$ Smarty-> debugging = false;

$ Smarty-> template_dir = 'templates/'; // The template directory can be customized as long as the directory does exist in the project
$ Smarty-> compile_dir = 'templates _ c/'; // The Directory of the compiled file is the same as the preceding one.

$ Smarty-> left_delimiter = '<! -- {'; // The specific code block on the left-flag parsing template page can be customized.
$ Smarty-> right_delimiter = '} -->'; // tag right

?>

MySQL. Class. php

<? PHP
/**
* MySQL Query Class
*
*/
Class dbquery {
/**
* Total number of queries
*
* @ Var int
*/
VaR $ querynum = 0;
/**
* Connection handle
*
* @ Var object
*/
VaR $ link;
 
/**
* Constructor
*
* @ Param string $ dbhost Host Name
* @ Param string $ dbuser
* @ Param string $ dbpw Password
* @ Param string $ dbname Database Name
* @ Param int $ whether pconnect is continuously connected
*/
Function dbquery ($ dbhost, $ dbuser, $ dbpw, $ dbname = '', $ pconnect = 0 ){
If ($ pconnect ){
If (! $ This-> link = @ mysql_pconnect ($ dbhost, $ dbuser, $ dbpw )){
$ This-> halt ('can not connect to MySQL Server ');
}
} Else {
If (! $ This-> link = @ mysql_connect ($ dbhost, $ dbuser, $ dbpw )){
$ This-> halt ('can not connect to MySQL Server ');
}
}
If ($ this-> Version ()> '4. 1 '){
Global $ dbcharset;
If ($ dbcharset ){
Mysql_query ("set character_set_connection = $ dbcharset, character_set_results = $ dbcharset, character_set_client = binary", $ this-> link );
}

If ($ this-> Version ()> '5. 0.1 '){
Mysql_query ("set SQL _mode ='' ", $ this-> link );
}
}

If ($ dbname ){
Mysql_select_db ($ dbname, $ this-> link );
}

}
/**
* Select a database
*
* @ Param string $ dbname
* @ Return
*/
Function select_db ($ dbname ){
Return mysql_select_db ($ dbname, $ this-> link );
}
/**
* Retrieve a record from the result set
*
* @ Param object $ Query
* @ Param int $ result_type
* @ Return Array
*/
Function fetch_array ($ query, $ result_type = mysql_assoc ){
Return mysql_fetch_array ($ query, $ result_type );
}
 
/**
* Querying SQL
*
* @ Param string $ SQL
* @ Param string $ type
* @ Return object
*/
Function query ($ SQL, $ type = ''){

$ Func = $ type = 'unbuffered' & @ function_exists ('mysql _ unbuffered_query ')?
'Mysql _ unbuffered_query ': 'mysql _ query ';
If (! ($ Query = $ func ($ SQL, $ this-> link) & $ type! = 'Silent '){
$ This-> halt ('mysql query error', $ SQL );
}

$ This-> querynum ++;
Return $ query;
}
/**
* Number of affected items
*
* @ Return int
*/
Function affected_rows (){
Return mysql_affected_rows ($ this-> link );
}
/**
* Error message returned
*
* @ Return Array
*/
Function error (){
Return ($ this-> link )? Mysql_error ($ this-> link): mysql_error ());
}
/**
* Return Error Code
*
* @ Return int
*/
Function errno (){
Return intval ($ this-> link )? Mysql_errno ($ this-> link): mysql_errno ());
}
/**
* Return query results
*
* @ Param object $ Query
* @ Param string $ row
* @ Return mixed
*/
Function result ($ query, $ row ){
$ Query = @ mysql_result ($ query, $ row );
Return $ query;
}
/**
* Number of results
*
* @ Param object $ Query
* @ Return int
*/
Function num_rows ($ query ){
$ Query = mysql_num_rows ($ query );
Return $ query;
}
/**
* Total number of fields
*
* @ Param object $ Query
* @ Return int
*/
Function num_fields ($ query ){
Return mysql_num_fields ($ query );
}
/**
* Release result set
*
* @ Param object $ Query
* @ Return bool
*/
Function free_result ($ query ){
Return mysql_free_result ($ query );
}
/**
* The auto-increment ID is returned.
*
* @ Return int
*/
Function insert_id (){
Return ($ id = mysql_insert_id ($ this-> link)> = 0? $ ID: $ this-> result ($ this-> query ("select last_insert_id ()"), 0 );
}
/**
* Retrieve a row from the result set as an enumerated Array
*
* @ Param object $ Query
* @ Return Array
*/
Function fetch_row ($ query ){
$ Query = mysql_fetch_row ($ query );
Return $ query;
}
/**
* Retrieve column information from the result set and return it as an object
*
* @ Param object $ Query
* @ Return object
*/
Function fetch_fields ($ query ){
Return mysql_fetch_field ($ query );
}
/**
* Returns the mysql version.
*
* @ Return string
*/
Function version (){
Return mysql_get_server_info ($ this-> link );
}
/**
* Close the connection.
*
* @ Return bool
*/
Function close (){
Return mysql_close ($ this-> link );
}
/**
* Output error message
*
* @ Param string $ message
* @ Param string $ SQL
*/
Function halt ($ message = '', $ SQL = ''){
Echo $ message. ''. $ SQL;
Exit;

}
}

?>

 

Userlist. php

<? PHP
Include_once 'common. php ';
$ Result = $ query-> query ("select * From userinfo ");
$ Userlist = array ();
While ($ ROW = $ query-> fetch_array ($ result ))
{
Array_push ($ userlist, $ row );
}
$ Query-> free_result ($ result );
$ Query-> close ();
$ Smarty-> assign ('userlist', $ userlist); // assign a value to the template
$ Smarty-> display('userlist.html '); // display the template content
?>

 

Userlist.html

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> User List </title>
</Head>
<Body>
<Center>
<Table>

<Tr> <TH> User Name </Th> <TH> password </Th> <TH> age </Th> <TH> remarks </Th> </tr>
<! -- {Foreach from = $ userlist item = user} -->
<Tr>
<TD> <! -- {$ User. Username} --> </TD>
<TD> <! -- {$ User. pwd} --> </TD>
<TD> <! -- {$ User. Age} --> </TD>
<TD> <! -- {$ User. remark} --> </TD>
</Tr>
<! -- {/Foreach} -->
</Table>
</Center>
</Body>
</Html>

 

 

Run userlist. php

User Name Password Age Remarks
Zhangsan 12345 20 AA
Liudehua Gggggg 40 AA

 

 

Test successful! In my personal summary, the template page is like a placeholder containing multiple smart tags and then performing dynamic operations. I am just a small demo, however, it seems that this method is a good solution for coupling to a certain extent, and you have to continue learning!

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.