Smarty quick start _ PHP Tutorial

Source: Internet
Author: User
Smarty Quick Start 2. To make it easier to use smarty, we can put the three steps "loading the Smarty template engine", "creating a Smarty object", and "setting the parameters of a Smarty object" in a public way to make it easier to use smarty in the future, we can put the three steps "loading the Smarty template engine", "creating a Smarty object", and "setting the parameters of a Smarty object" in a public php file, in the future, we can reuqire it directly, for example:

1. create a main. php

Include smarty/Smarty. class. php;
// The next time the program is transplanted, you only need to modify the ROOT point location ~
Const DIR_SEP = DIRECTORY_SEPARATOR;
Define (ROOT, D:. DIR_SEP._PHP _ Test. DIR_SEP.Test2.DIR_SEP); $ tpl = new Smarty ();
$ Tpl-> template_dir = ROOT. templates. DIR_SEP;
$ Tpl-> complie_dir = ROOT. templates_c.DIR_SEP;
$ Tpl-> config_dir = ROOT. configs. DIR_SEP;
$ Tpl-> cache_dir = ROOT. cache. DIR_SEP;
$ Tpl-> left_delimiter = <{;
$ Tpl-> right_delimiter =}>;
?>

Note 1: Why is DIRECTORY_SEPARATOR used here? (Click to view details)

Note 2: left_delimiter and right_delimiter are left and right Terminator variables, which can be defined as other strings (default {}).

2. create a new test.htm under templates.




<{$ Title}>


<{$ Content}>

3. call the template page to fill in the title and content, and create a new test_smarty_1.php

Require main. php;
$ Tpl-> assign (title, New Message );
$ Tpl-> assign (content, This is test smarty !);
$ Tpl-> display(test.htm );
?>

You can also write it like this.

Require main. php;
$ Tpl-> assign (array (title => NewMessage, content => This is test smarty !));
$ Tpl-> display(test.htm );
?>

Output result:

Page title Display: NewMessage

This is test smarty!

The following three steps are taken: Create a Smarty object and set the parameters of a Smarty object...

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.