After solving the character verification problem that plagued the day, I found a smarty nested call problem in csdn.
G analyzed several articles.
Assume that there are four files. The PHP files are in the same directory and the same names match each other (PHP + template)
Index. php
Index. TPL
Header. php
Header. TPL
Nested structure 1
Index. php
│
─ ── Index. TPL
│
└ ── Header. php
│
└ ── Header. TPL
The benefit of nested structure: the header is not related to the index and can be easily modified. The Code is as follows:
Index. php
<? PHP
/* Template settings */
Require_once ($ _ server ["document_root"]. '/smarty. Class. php ');
$ Smarty = new smarty ();
$ Smarty-> template_dir = './include/templates ';
$ Smarty-> compile_dir = './include/templates_c ';
$ Smarty-> cache_dir = './include/caccache ';
$ Smarty-> config_dir = './include/config ';
/*************************************** *******************/
$ Index = "Homepage ";
$ Smarty-> assign ('index', $ index );
$ Smarty-> display (index. TPL );
?>
Index. TPL
<HTML>
{Include_php file = "./header. php "}
<Body >{$ index} </body>
</Html>
Header. php
<? PHP
$ Header = "$ Smarty-> assign ('header', $ header );
$ Smarty-> display (header. TPL );
?>
Header. TPL
{$ Header}
----------------------------
Nested structure 2
Index. php
│
├ ── Header. php
─ ── Index. TPL
└ ── Header. TPL
Although the Code can run in this structure, because the module and template are not supporting calls, maintenance is time-consuming and laborious, and sometimes the encoding structure is damaged. The Code is as follows:
Index. php
<? PHP
/* Template settings */
Require_once ($ _ server ["document_root"]. '/smarty. Class. php ');
$ Smarty = new smarty ();
Require_once ("./header. php ");
// Note the location. Assign () on a non-object error will occur when referencing the file at the beginning. After configuring the smarty statement, save the smarty settings to the smarty. config. PHP, and calls in requrie header. before PHP
$ Smarty-> template_dir = './include/templates ';
$ Smarty-> compile_dir = './include/templates_c ';
$ Smarty-> cache_dir = './include/caccache ';
$ Smarty-> config_dir = './include/config ';
/*************************************** *******************/
$ Index = "Homepage ";
$ Smarty-> assign ('index', $ index );
$ Smarty-> display ('index. TPL ');
?>
Index. TPL
<HTML>
{Include file = "header. TPL "}
<Body >{$ index} </body>
</Html>
Header. php
<? PHP
$ Header = "Header ";
$ Smarty-> assign ('header', $ header );
?>
Header. TPL
<Head >{$ header}
----------------------------
Nested structure 3
Index. php
│
├ ── Header. php
│ ─ ── Header. TPL
─ ── Index. the third structure of TPL can run code, but it is the most troublesome to maintain and seriously damages the template's encoding structure. When you want to change the code, you have to query the matching HTML tags on the two pages. The Code is as follows:
Index. php
<? PHP
/* Template settings */
Require_once ($ _ server ["document_root"]. '/smarty. Class. php ');
$ Smarty = new smarty ();
Require_once ("./header. php ");
// Note the location. The assign () on a non-object error will occur when the file is referenced at the beginning. The statement must be after new smarty (). Of course, you also save the smarty settings to smarty. config. PHP, and calls in requrie header. before PHP
$ Smarty-> template_dir = './include/templates ';
$ Smarty-> compile_dir = './include/templates_c ';
$ Smarty-> cache_dir = './include/caccache ';
$ Smarty-> config_dir = './include/config ';
/*************************************** *******************/
$ Index = "Homepage ";
$ Smarty-> assign ('index', $ index );
$ Smarty-> display ('index. TPL ');
?>
Index. TPL
<Body >{$ index} </body>
</Html>
Header. php
<? PHP
$ Header = "Header ";
$ Smarty-> assign ('header', $ header );
$ Smarty-> display ('header. TPL ');
?>
Header. TPL
<HTML>
<Head >{$ header}
----------------------------
Conclusion: we can see from the above three structures that if you use a template, it is best to map the PHP file with the matching template, which is convenient for modification, and the code integrity will not be paid...
(Due to the deep research on smarty, we hope to understand that there are still imperfections)