The syntax format for the Smarty template contains files is as follows: {include file= $filename variable= $value ...} where $filename is the file name of the file to include, variable and value is a variable setting that replaces the keyword in the included file.
An example of a simple include file, where test.htm is a template file, and inc.html is a static HTML templates file that is included.
The code is as follows |
Copy Code |
$smarty = new Smarty; $smarty->display (' test.html '); $smarty = new Smarty; $smarty->display (' test.html '); The HTML code for the test.html file looks like this: <title>inc file</title> <body> {include file= "inc.html"} </body> <title>inc file</title> <body> {include file= "inc.html"} </body>
|
Run to see that the contents of the included files are displayed.
Of course, this file name can also be replaced with variables, such as the following
The code is as follows |
Copy Code |
$smarty = new Smarty; $inc _name = "inc.html"; $smarty->assign ("Inc_name", $inc _name); Replace variable $smarty->display (' test.html '); $smarty = new Smarty; $inc _name = "inc.html"; $smarty->assign ("Inc_name", $inc _name); Replace variable $smarty->display (' test.html '); run the same result as before.
|
If the included file is a template, you need to specify the variable by using the template that invokes the template file. Rewrite the previous inc.htm as follows:
The code is as follows |
Copy Code |
$smarty = new Smarty; $inc _name = "inc.html"; $inca = "I am a variable contained within a file"; $smarty->assign ("Inc_name", $inc _name); Replace variable $smarty->assign ("Inca", $inca); Replace the variables in the included file $smarty->display (' test.html '); $smarty = new Smarty; $inc _name = "inc.html"; $inca = "I am a variable contained within a file"; $smarty->assign ("Inc_name", $inc _name); Replace variable $smarty->assign ("Inca", $inca); Replace the variables in the included file $smarty->display (' test.html '); |
The test.html template file looks like this:
The code is as follows |
Copy Code |
<title>inc file</title> <body> {include file= $inc _name inca= $inca} </body> <title>inc file</title> <body> {include file= $inc _name inca= $inca} </body> |
Inc.html is included in the file code:
{$inca} {$inca} After running, you can see that the variable containing the file is also replaced, very convenient.