Introduction to smarty Templates

Source: Internet
Author: User
PHP code: ------------------------------------------------------------------------------ the following section is based on version 2.5. I don't know if there is a BUG. the OS is win2000. php should be 4.1 or ., I have used it several times during development, but I have not studied it too deeply. I can find it in the manual. if there is an error, it is inevitable that SMARTY

PHP code :--------------------------------------------------------------------------------



The introduction below is based on version 2.5, and I do not know if there is a BUG. the OS is win2000, and php should

4.1 and later ., I used it several times during development, but I didn't study it too deeply,

The introduction can be found in the manual. if there is any error, it is inevitable that I will not be sorry.


The smarty template is said to be recommended by php.net. it is said that it has strong functions and is said to be fast.

It is also not slow, it is said that more and more people are used

The official website smarty.php.net contains forums, downloads, and manuals. If you have everything you want, go to the topic:
(-) Installation:
After the downloaded package is decompressed, there are three sub-directories: The template file directory is There are four class files under a directory. first, we will introduce the most important: Smarty. class. php should be the core class of the whole smarty template. it is also used to deal with it in normal times. The following directory structure is created under your application directory:
/App_1/smarty/libs
/App_1/smarty/templates_c
/App_1/smarty/templates
/App_1/smarty/configs

The class files required by smarty are stored in the libs Directory of the compressed package.
Store template files. the template files used by the program are stored here.
Store the module property file, which will be detailed later
Store the "compiled" template file. later, we will detail that some directories need to be set to 777.
Save cached template files

The above file name is the default file name of smarty. you can specify different file names, such as guest_template_dir and admin_template_dir. if not specified, the above file name will be used.
(2) usage:

Design template:
1 Template variable: {$ variable name}, for example, {$ color}, {$ type}
Test_color.tpl
{$ Name}
Test_url.tpl
{$ Title}

2. you can also use a template array.
Test_array.tpl
{People. name}

{People. sex}

{People. money}

3. list of blocks used by the template
For example:
User1 user‑sex user‑money
User2 user2_sex user2_money
User3 user3_sex user3_money

Section. tpl
You can format the table:








{Section name = user loop = $ userList} {/Section}
{$ UserList [user]. name} {$ UserList [user]. sex} {$ UserList [user]. money}



// Under app_1 of the current directory
// Generate $ smarty instance

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

// Specify the function directory, which can be customized
$ Smarty-> template_dir = 'smarty/templates ';
$ Smarty-> $ compile_dir = 'smarty/template_c ';

// Assign a value to the template variable Template: color. tpl is stored in smarty/templates.
// $ Smarty-> assign ('Template variable name', 'php internal variable ');
// $ Smarty-> display (template file name );

$ Smarty-> assign ('color', 'red ');
$ Smarty-> assign ('name', 'Hello World ');

// Display template
// Output: helloworld
$ Smarty-> display ('test _ color. tpl ');

// Assign values to the template array variable. Template: test_array.tpl
// Output:

// Ba dou
// Male
// A litte
$ People = array ('name' => 'badou ', 'sex' => 'male', 'Money' => 'A little ');
$ Smarty-> assign ('people', $ people );
$ Smarty-> display ('test _ color. tpl ');

// Template block
// {Section name = user loop = $ userList}
// Section: Label function
// Name: Tag name
// Loop: loop array
// Function cyclically outputs multiple rows
// Output:

// User1 userpolicsex user=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 ');
?>
Writing this item is the most basic thing for the time being, and the code is not tested. I am not responsible for any errors in my first writing.
There are also a lot of interesting functions to be written later

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.