Smarty template contains calls to other template files

Source: Internet
Author: User
Tags smarty template

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.

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.