In order to use smarty more conveniently in the future, we can put the three steps of "loading the Smarty template engine", "creating the Smarty object" and "setting the parameters of the Smarty object" into a public php file. We can just reuqire the place where it is used, for example:
1. Create a main.php
include smarty/Smarty.class.php;
//You only need to modify the ROOT pointing position next time the program is transplanted~
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 use DIRECTORY_SEPARATOR here? (Click to view)
Note 2: left_delimiter and right_delimiter are left and right terminator variables, which can be defined as other strings (default is {}).
2. Under templates, create a new test.htm
3. Call the template page to fill in the title and content, and create a new test_smarty_1.php
require main.php;
$tpl->assign(title, New Message);
$tpl->assign(content, This is test smarty!);
$tpl->display(test.htm);
?>
You can also write like this
require main.php;
$tpl->assign(array(title=>NewMessage, content=>This is test smarty!));
$tpl ->display(test.htm);
?>
Output result:
The page title displays: NewMessage
The page content shows: This is test smarty!