Heim  >  Artikel  >  Backend-Entwicklung  >  Smarty快速入门之二_PHP教程

Smarty快速入门之二_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:43:49793Durchsuche

为了以后更方便的使用smarty,我们可以将“加载Smarty 模版引擎”、“建立 Smarty 对象”、“设定Smarty 对象的参数”这三步放到一个公共的php文件内,以后在需要使用的地方我们直接reuqire一下,即可,例如:

1. 建立一个main.php

include smarty/Smarty.class.php;
//下次程序移植时只需要修改ROOT指向位置即可~
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 = }>;
?> 

注1:此处为何要使用DIRECTORY_SEPARATOR?(点击查看)

注2:left_delimiter和right_delimiter为左右结束符变量,此处可定义为其他字符串(默认为{})。

2. 在templates下,新建立一个test.htm

 






 

3. 调用模板页给title和content填充内容,新建一个test_smarty_1.php

require main.php;
$tpl->assign(title, New Message);
$tpl->assign(content, This is test smarty!);
$tpl->display(test.htm);
?>

也可以这样写

require main.php;
$tpl->assign(array(title=>NewMessage, content=>This is test smarty!));
$tpl->display(test.htm);
?>

输出结果:

页面title显示:NewMessage

页面内容显示:This is test smarty!

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478801.htmlTechArticle为了以后更方便的使用smarty,我们可以将“加载Smarty 模版引擎”、“建立 Smarty 对象”、“设定Smarty 对象的参数”这三步放到一个公共的...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn