Introducing the Smarty Template

Source: Internet
Author: User
Tags array php code variable smarty template
Template PHP Code:--------------------------------------------------------------------------------



The following introduction is based on the 2.5 version, also do not know that there is no bug, OS is win2000,php should

After 4.1, it's OK. , brothers were only used a few times to develop, not too deep to study,

The introduction of the manual can be found, if there are mistakes in the inevitable to forgive it


Smarty templates are said to be recommended for use by php.net, which is said to function relatively strongly, and is said to speed

Also not slow, is said to use more and more people

The official website smarty.php.net above has the forum, has the download, has the handbook. Have everything you want, cut to the chase:
(-) Installation:
The downloaded package has three subdirectories after decompression: The template file directory is a directory of four class files under <libs>. First of all, introduce the most important: Smarty.class.php It should be the core class of the entire Smarty template, which is used to deal with it, Create the following directory structure under your application directory:
/app_1/smarty/libs
/app_1/smarty/templates_c
/app_1/smarty/templates
/app_1/smarty/configs

<libs> corresponding to the compression package under the Libs directory to put Smarty required class files
<templates> Store template files, the template files used in the program are put here
<configs> Storage Mode properties file, later in detail.
<templates_c> Store The "compiled" template file, later in detail, some directories need to be set to 777
<cache> storage of cached template files

The above file name is smarty default filename, the user can specify a different file name, such as: Guest_template_dir,admin_template_dir. If you do not specify that the file name will be used
(ii) Use:

Design Template:
1 Template variable: {$ variable name}, such as {$color},{$type}
Test_color.tpl
<font color={$color}>{$name}</font>
Test_url.tpl
<a href= ' {$url} ' target= ' {$target} ' >{$title}</a>

2 You can also use the template array
Test_array.tpl
{people.name}<br>
{people.sex}<br>
{people.money}<br>
3 Template use block List
Such as:
User1 User1_sex User1_money
User2 User2_sex User2_money
User3 User3_sex User3_money

Section.tpl
Can be formatted with a table:
<TABLE>
{section Name=user loop= $userList}
<TR>
<td>{$userList [user].name}</td>
<td>{$userList [user].sex}</td>
<td>{$userList [user].money}</td>
</TR>
{/section}
</TABLE>


<?php
Current directory App_1 under
Generate $smarty Instance

Require (' smarty/lib/smarty.class.php ');
$smarty = new Smarty;

Specifies the feature catalog, which you can customize
$smarty->template_dir = ' smarty/templates ';
$smarty-> $compile _dir = ' Smarty/template_c ';

Assign a template to a template variable: Color.tpl placed under Smarty/templates
$smarty->assign (' template variable name ', ' PHP internal variable ');
$smarty->display (template file name);

$smarty->assign (' Color ', ' red ');
$smarty->assign (' name ', ' Hello World ');

Show templates
Output: <font color=red>helloworld</font>
$smarty->display (' Test_color.tpl ');

Assign a value to a template array variable, Template: Test_array.tpl
Output:

Badou
Man
A litte
$people = Array (' name ' => ' crotonic ', ' sex ' => ' man ', ' money ' => ' a little ');
$smarty->assign (' People ', $people);
$smarty->display (' Test_color.tpl ');

Template blocks
{section Name=user loop= $userList}
Section: Label function
Name: Label name
Loop: Looping arrays
function loops output Multiple lines
Output:

User1 User1_sex User1_money
User2 User2_sex User2_money
User3 User3_sex User3_money


$userList [] = Array (' name ' => ' user1 ', ' sex ' => ' user1_sex ', ' money ' => ' User1_money ');
$userList [] = Array (' name ' => ' user2 ', ' Sex ' => ' user2_sex ', ' money ' => ' User2_money ');
$userList [] = Array (' name ' => ' user3 ', ' Sex ' => ' user3_sex ', ' money ' => ' User3_money ');

$smarty->assign (' userlist ', $userList);
$smarty->display (' Test_section.tpl ');
?>
Write this point for the moment is the most basic things, the code did not test, the brother first write something wrong place one is not responsible.
There are a lot of interesting features and then have the opportunity to write


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.