Analysis on modified usage of smarty variables in php

Source: Internet
Author: User

Analysis on modified usage of smarty variables in php

Test. php code:

1

2

3

4

5

6

7

8

9

<? Php

Require 'libs/Smarty. class. php'; // contains the Smarty class library file

$ Smarty = new Smarty; // create a new Smarty object

$ Total = 12345; // assign a value to $ total

$ Smarty-> assign ("total", $ total); // assign values to variables in the template

$ Formatted_total = number_format ($ total); // format $ total

$ Smarty-> assign ("formatted_total", $ formatted_total); // assign values to variables in the template

$ Smarty-> display('test1.htm'); // display page

?>

Code of the test1.html template:

1

2

3

4

5

6

7

8

9

<Html>

<Head>

<Title> Smarty Test </title>

</Head>

<Body>

<H1> Total is {$ total} </H1>

<H1> Formatted Total is {$ formatted_total} </H1>

</Body>

</Html>

Compiled test.html. php code:

1

2

3

4

5

6

7

8

9

10

11

12

13

<? Php/* Smarty version 2.6.22, created on 14:37:39

Compiled from test1.htm */?>

<Html>

<Head>

<Title> Smarty Test </title>

</Head>

<Body>

<H1> Total is <? Php echo $ this-> _ tpl_vars ['Total'];?>

</H1>

<H1> Formatted Total is <? Php echo $ this-> _ tpl_vars ['formatted _ total'];?>

</H1>

</Body>

</Html>

The test1.html template can be rewritten as test2.html:

1

2

3

4

5

6

7

8

9

<Html>

<Head>

<Title> Smarty Test </title>

</Head>

<Body>

<H1> Total is {$ total} </H1>

<H1> Formatted Total is {$ total | number_format} </H1>

</Body>

</Html>

The corresponding test. php code is changed:

1

2

3

4

5

6

7

<? Php

Require 'libs/Smarty. class. php'; // contains the Smarty class library file

$ Smarty = new Smarty; // create a new Smarty object

$ Total = 12345;

$ Smarty-> assign ("total", $ total); // assign values to variables in the template

$ Smarty-> display('test2.htm'); // display page

?>

Browser display:

Total is 12345
Formatted Total is 12,345

I hope this article will help you with php programming.

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.