Getting started with Smarty

Source: Internet
Author: User
Note the following points: 1. the position of the sample is based on the previously defined template_dir. 2. the variables of all included subsamples will also be interpreted .; 3. you can use "variable name = variable content" in "include" to specify the variables contained in the introduced sample, as in example 4 above.

Preface

Region content

  

  

Most of these blocks use if or for and while in PHP programs to control their display status. Although the sample version looks much simpler, you only need to change the display mode of the sample version, PHP programs must be modified again! Main. php:

Php code:

Include "class/Smarty. class. php ";
Define ('_ SITE_ROOT', 'd:/appserv/web/Demo'); // no diagonal lines at last
$ Tpl = new Smarty ();
$ Tpl-> template_dir = _ SITE_ROOT. "/templates /";
$ Tpl-> compile_dir = _ SITE_ROOT. "/templates_c /";
$ Tpl-> config_dir = _ SITE_ROOT. "/configs /";
$ Tpl-> cache_dir = _ SITE_ROOT. "/cache /";
$ Tpl-> left_delimiter = '<{';
$ Tpl-> right_delimiter = '}> ';
?>

 

The purpose set in the above method is that if the program is to be transplanted to other places, you only need to change _ SITE_ROOT. (See XOOPS) templates/test.htm:

 

Php code:



<{$ Title}>


<{$ Content}>


Now we want to display the above sample and change the webpage title ($ title) and content ($ content). please name the following content as test. php and put it in the main folder: test. php:

Php code:

Require "main. php ";
$ Tpl-> assign ("title", "webpage title for testing ");
$ Tpl-> assign ("content", "tested webpage content ");
// The above two rows can also be replaced by this line
// $ Tpl-> assign (array ("title" => "webpage title for testing", "content" => "webpage content for testing "));
$ Tpl-> display('test.htm ');
?> Templates_c/% 179/% 1798044067/test.htm. php:



<? Php echo $ this-> _ tpl_vars ['title'];?>


_ Tpl_vars ['content'];?>


That's right. this is what Smarty compiled. It converts the variables in the sample version into PHP syntax for execution. The next time we read the same content, Smarty will directly capture this file for execution. Main. php:

Php code:

Include "class/Smarty. class. php ";
Define ('_ SITE_ROOT', 'd:/appserv/web/Demo'); // no diagonal lines at last
// Base on the location of main. php
Require_once "des/functions. php ";
Require_once "des/include. php ";
$ Tpl = new Smarty ();
$ Tpl-> template_dir = _ SITE_ROOT. "/templates /";
$ Tpl-> compile_dir = _ SITE_ROOT. "/templates_c /";
$ Tpl-> config_dir = _ SITE_ROOT. "/configs /";
$ Tpl-> cache_dir = _ SITE_ROOT. "/cache /";
$ Tpl-> left_delimiter = '<{';
$ Tpl-> right_delimiter = '}> ';
?>


 

The modules folder is used to place program modules. in this way, the program will not be lost.

1. <{$ var}>

2. <{$ var}>

3. <{$ var}>

In Smarty, the variable defaults to global, that is, you only need to specify it once. If you specify more than two times, the variable content is based on the last one specified. Even if we load external subsample in the main sample, the same variables in the subsample will also be replaced, so that we no longer need to parse the subsample. <{Variable | MODIFIER}>

<{Variable | modifier: "parameter (optional, depending on the function)"}>

  

Example:

Php code:

<{$ Var | nl2br}>
<{$ Var | string_format: "% 02d"}>


 

Total amount: 21,000 yuan

  

The sample engine may be written as follows:

Total amount: {format_total} RMB

  
Php code:

$ Total = 21000;
$ Tpl-> assign ("total", $ total );
$ Tpl-> assign ("format_total", number_format ($ total ));
?>

The sample version of Smarty can be written as follows: (the number_format modifier can be downloaded from the official Smarty webpage)

Total amount: <{$ total | number_format: ""}> RMB

Php code:

$ Total = 21000;
$ Tpl-> assign ("total", $ total );
?>

Therefore, in Smarty, we only need to specify a variable, and the rest can be decided by the sample version. Have you understood this? This is the benefit of making the sample version decide the display style of variables on its own!


  

Control the content of the sample

The covered block test2.php:

Php code:

Require "main. php ";
$ Array1 = array (1 => "apple", 2 => "pineapple", 3 => "banana", 4 => "Ba Le ");
$ Tpl-> assign ("array1", $ array1 );
$ Array2 = array (
Array ("index1" => "data1-1", "index2" => "data1-2", "index3" => "data1-3 "),
Array ("index1" => "data2-1", "index2" => "data2-2", "index3" => "data2-3 "),
Array ("index1" => "data3-1", "index2" => "data3-2", "index3" => "data3-3 "),
Array ("index1" => "data4-1", "index2" => "data4-2", "index3" => "data4-3 "),
Array ("index1" => "data5-1", "index2" => "data5-2", "index3" => "data5-3 "));
$ Tpl-> assign ("array2", $ array2 );
$ Tpl-> display ("test2.htm ");
?>


 

The sample is written as follows: templates/test2.htm:

Php code:




Test duplicate block



Use foreach to present array1
<{Foreach item = item1 from = $ array1}>
<{$ Item1}>
<{/Foreach}>
Use section to present array1
<{Section name = sec1 loop = $ array1}>
<{$ Array1 [sec1]}>
<{/Section}>
Use foreach to present array2
<{Foreach item = index2 from = $ array2}>
<{Foreach key = key2 item = item2 from = $ index2}>
<{$ Key2 }>:< {$ item2}>
<{/Foreach}>
<{/Foreach}>
Use section to present array1
<{Section name = sec2 loop = $ array2}>
Index1: <{$ array2 [sec2]. index1}>
Index2: <{$ array2 [sec2]. index2}>
Index3: <{$ array2 [sec2]. index3}>
<{/Section}>



Test3.php: Require "main. php ";
$ Forum = array (
Array ("category_id" => 1, "category_name" => "announcement area ",
"Topic" => array (
Array ("topic_id" => 1, "topic_name" => "website announcement ")
)
),
Array ("category_id" => 2, "category_name" => "文 ",
"Topic" => array (
Array ("topic_id" => 2, "topic_name" => "Good book introduction "),
Array ("topic_id" => 3, "topic_name" => "surprise ")
)
),
Array ("category_id" => 3, "category_name" => "computer area ",
"Topic" => array (
Array ("topic_id" => 4, "topic_name" => "hardware trimming "),
Array ("topic_id" => 5, "topic_name" => "software discussion ")
)
)
);
$ Tpl-> assign ("forum", $ forum );
$ Tpl-> display ("test3.htm ");
?>


 

The sample is written as follows: templates/test3.htm:

Php code:



Nested incircle test














<{Section name = sec1 loop = $ forum}>           <{Section name = sec2 loop = $ forum [sec1]. topic}>                <{/Section}><{/Section}>
<{$ Forum [sec1]. category_name}>
 <{$ Forum [sec1]. topic [sec2]. topic_name}>



Test3.php:

Php code:

Require "main. php ";
// First create the first layer of array
$ Category = array ();
$ Db-> setSQL ($ SQL1, 'Category ');
If (! $ Db-> query ('Category ') die ($ db-> error ());
// Capture the data in the first round
While ($ item_category = $ db-> fetchAssoc ('Category '))
{
// Create a layer 2 array
$ Topic = array ();
$ Db-> setSQL (sprintf ($ SQL2, $ item_category ['Category _ id']), 'topic ');
If (! $ Db-> query ('topic ') die ($ db-> error ());
// Capture data in the second round
While ($ item_topic = $ db-> fetchAssoc ('topic '))
{
// Push the captured data into the second layer array
Array_push ($ topic, $ item_topic );
}
// Specify the second layer array as a member of the data captured by the first layer array.
$ Item_category ['topic '] = $ topic;
// Push the first layer of data into the first layer of array
Array_push ($ category, $ item_category );
}
$ Tpl-> assign ("forum", $ category );
$ Tpl-> display ("test3.htm ");
?>
After capturing a piece of data in the database, we get an array containing the data. Using the while statement and the array_push function, we can insert the data in the database into the array. If you only use a single-layer primary ring, remove the second-layer secondary ring (the red part.

Determines whether the content is displayed <{if $ is_login = true}>


Show user operation menus

<{Else}>

Display the input account and password form

<{/If}>

  

Note that there must be at least one blank character on both sides of the "=" sign; otherwise, the Smarty cannot be parsed. Test4.php:

Php code:

Require "main. php ";
$ My_array = array (
Array ("value" => "0 "),
Array ("value" => "1 "),
Array ("value" => "2 "),
Array ("value" => "3 "),
Array ("value" => "4 "),
Array ("value" => "5 "),
Array ("value" => "6 "),
Array ("value" => "7 "),
Array ("value" => "8 "),
Array ("value" => "9 "));
$ Tpl-> assign ("my_array", $ my_array );
$ Tpl-> display('test4.htm ');
?>


 

The sample is written as follows: templates/test4.htm:

Php code:



Lateral relay table test












   <{Section name = sec1 loop = $ my_array}>     <{If $ smarty. section. sec1.rownum is div by 2}>      <{/If}><{/Section}>  
<{$ My_array [sec1]. value}>



Test5.php: Require "main. php ";
$ Tpl-> assign ("title", "Include test ");
$ Tpl-> assign ("content", "This is a variable in sample 2 ");
$ Tpl-> assign ("dyn_page", "test5_3.htm ");
$ Tpl-> display('test5_1.htm ');
?>


 

Sample 1 is written as follows: templates/test5_1.htm:

Php code:




<{$ Title}>


<{Include file = "test5_2.htm"}>

<{Include file = $ dyn_page}>
<{Include file = "test5_4.htm" custom_var = "custom variable content"}>


Templates/test5_2.htm: <{$ content}>

Sample 3 is written as follows: templates/test5_3.htm: this is the content of sample 3.


Templates/test5_4.htm: <{$ custom_var}>

  

Note the following points: 1. the position of the sample is based on the previously defined template_dir. 2. the variables of all included subsamples will also be interpreted .; 3. you can use "variable name = variable content" in "include" to specify the variables contained in the introduced sample, as in example 4 above.

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.