Instance 1:
Let's look at a simple example.
Index. tpl
CODE: [Copy to clipboard] {* the text contained in * is the comment content in the smarty variable identifier *}
<{Include file = "header. tpl"}> {* page header *}
Hello everyone, my name is <{$ name}>. You are welcome to read my smarty learning materials.
<{Include file = "foot. tpl"}> {* end of page *}
The above example is a tpl template, where:
1. <{**}> is the template page comment. It does not output any output when the smarty parses the template. It is only used by the template designer to comment on the template.
2. <{include file = "xxx. tpl "}> use this sentence to include a template file to the current page. In this example, the head is used in public services on the website. tpl and foot. tpl is included. You can
In this case, copy all the content in xxx. tpl to the current statement. Of course, you can copy the content in XXX. tpl to the current statement without using this sentence.
That's all right.
3. {$ name}: template variable, which is the core component of smarty. It is included in the left boundary {and right boundary} defined by smarty and provided in the form of a PHP variable. It will be used in the smarty program.
$ Smarty-> assign ("name", "Li Xiaojun"); replace the $ name in the template with the word "Li Xiaojun.
The entire instance source code is as follows:
Header. tpl
CODE: [Copy to clipboard] <Head>
<Title> Master smarty tutorial </title>
</Head>
<Body>
Foot. tpl
CODE: [Copy to clipboard] <Center> CopyRight (C) by Email: teacherli@163.com August 2004 </center>
<Hr>
</Body>
</Html>
Index. tpl
CODE: [Copy to clipboard] {* the text contained in * is the comment content in the smarty variable identifier *}
{Include file = "header. tpl"} {* page header *}
Hello everyone, my name is {$ name}. You are welcome to read my smarty learning materials.
{Include file = "foot. tpl"} {* end of page *}
Index. php
PHP: [Copy to clipboard]
<? Php
/*************************************** ******
*
* File name: index. php
* For use: Display instance programs
*
* Author: Master
* Email: teacherli@163.com
* Rectification: forest
**************************************** *****/
Include_once ("./comm/Smarty. class. php"); // contains the smarty class file
$ Smarty = new Smarty (); // Create a smarty instance object $ smarty
$ Smarty-> template_dir = "./templates"; // set the template directory
$ Smarty-> compile_dir = "./templates_c"; // Set the compilation Directory
//----------------------------------------------------
// Left and right boundary characters. The default value is {}.
// Conflict, so it is recommended to set it to <{}> or another.
//----------------------------------------------------
$ Smarty-> left_delimiter = "<{";
$ Smarty-> right_delimiter = "}> ";
$ Smarty-> assign ("name", "Li Xiaojun"); // replace Template variables
// Compile and display the index. tpl template under./templates
$ Smarty-> display ("index. tpl ");
?>
The final execution of this program will be shown:
======================================
Execute index. php
======================================
<Html>
<Head>
<Title> Master smarty tutorial </title>
</Head>
<Body>
Hello everyone, my name is Li Xiaojun. You are welcome to read my smarty learning materials.
<Hr>
<Center> CopyRight (C) by August 2004 <center>
<Hr>
</Body>
</Html>