Simple php smarty entry program instance
First, you must have three folders: configs, templates, and templates_c. In the configs folder, there is a configuration file: test. conf. The Code is as follows:
|
1
2
3
4 |
Title = Welcome to Smarty!
Cutoff_size = 40
[Setup]
Bold = true
|
Templates has the template file test.htm:
|
1
2
3
4
5
6
7
8 |
<Html>
<Head>
<Title> Smarty Test </title>
</Head>
<Body>
<H1> Hello, {$ Name} </H1>
</Body>
</Html>
|
PHP file code:
|
1
2
3
4
5
6 |
<? Php
Require 'libs/Smarty. class. php'; // contains the Smarty class library file
$ Smarty = new Smarty; // create a new Smarty object
$ Smarty-> assign ("Name", "Simon"); // assign values to variables in the template
$ Smarty-> display('test.htm'); // display page
?>
|
Page code displayed after running:
|
1
2
3
4
5
6
7
8 |
<Html>
<Head>
<Title> Smarty Test </title>
</Head>
<Body>
<H1> Hello, Simon </H1>
</Body>
</Html>
|
After running, a PHP file is generated in the templates_c Folder:
|
1
2
3
4
5
6
7
8
9
10
11
12
13 |
<? Php
/* Smarty version 2.6.22, created on 13:20:00
Compiled from test.htm */
?>
<Html>
<Head>
<Title> Smarty Test </title>
</Head>
<Body>
<H1> Hello, <? Php echo $ this-> _ tpl_vars ['name'];?>
</H1>
</Body>
</Html>
|
This file shows the effect of browsing.