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.