To make it easier to use smarty, we can put the three steps "loading the Smarty template engine", "Creating a Smarty object", and "setting the parameters of a Smarty object" in a public PHP file, in the future, we can reuqire it directly, for example:
1. Create a main. php
<? Php
Include smarty/Smarty. class. php;
// The next time the program is transplanted, you only need to modify the ROOT point location ~
Const DIR_SEP = DIRECTORY_SEPARATOR;
Define (ROOT, D:. DIR_SEP._PHP _ Test. DIR_SEP.Test2.DIR_SEP); $ tpl = new Smarty ();
$ Tpl-> template_dir = ROOT. templates. DIR_SEP;
$ Tpl-> complie_dir = ROOT. templates_c.DIR_SEP;
$ Tpl-> config_dir = ROOT. configs. DIR_SEP;
$ Tpl-> cache_dir = ROOT. cache. DIR_SEP;
$ Tpl-> left_delimiter = <{;
$ Tpl-> right_delimiter =}>;
?>
Note 1: Why is DIRECTORY_SEPARATOR used here? (Click to view details)
NOTE 2: left_delimiter and right_delimiter are left and right Terminator variables, which can be defined as other strings (default {}).
2. Create a New test.htm under templates.
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> <{$ title}> </title>
</Head>
<Body>
<{$ Content}>
</Body>
</Html>
3. Call the template page to fill in the title and content, and create a new test_smarty_1.php
<? Php
Require main. php;
$ Tpl-> assign (title, New Message );
$ Tpl-> assign (content, This is test smarty !);
$ Tpl-> display(test.htm );
?>
You can also write it like this.
<? Php
Require main. php;
$ Tpl-> assign (array (title => NewMessage, content => This is test smarty !));
$ Tpl-> display(test.htm );
?>
Output result:
Page title display: NewMessage
This is test smarty!