Home  >  Article  >  Backend Development  >  Smarty Quick Start Part 2_PHP Tutorial

Smarty Quick Start Part 2_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:43:49797browse

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







<{$content}>

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!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478801.htmlTechArticleIn order to use smarty more conveniently in the future, we can change "Load Smarty Template Engine", "Create Smarty Object", Put these three steps of "setting parameters of Smarty object" into a common...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn