助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
  要用smarty模板引擎,首先要下载smarty,笔者在百度网盘上存的smarty3.1.28,[点此下载](https://pan.baidu.com/s/1boDE5n5)。   下载解压,其中有一个libs目录,将此目录里的文件复制到www/cart/smarty目录里(在www里建立smarty目录)。   另外,必须cart根目录再建4个目录, 这4个目录是使用smarty引擎必须的,分别是templates、 templates_c、configs、cache。    templates:用于存放模板文件。   templates_c:用于存放编译后的模板文件。   configs:用于存放配置文件。   cache:模板文件缓存目录。 * * * * *   在www/cart根目录建一个config.php文件,其中代码如下: ~~~ <?php //加载smarty核心文件. require ('smarty/Smarty.class.php'); //实例化一个Smart对象. $smarty=new Smarty; //指定模板文件的存储位置. $smarty->template_dir='templates'; //指定模板编译文件存储位置. $smarty->compile_dir='templates_c'; //指定配置文件存储位置. $smarty->config_dir='configs'; //指定缓存文件存储位置. $smarty->cache_dir='cache'; ?> ~~~   因4个目录与smarty核心目录都在根目录下,所以配置代码简单。 * * * * *   测试smarty配置是否成功。   1、在www/cart/建立index.php *养成写一段代码,就进行测试的习惯,这样不致于写几十行代码,运行时出错,很郁闷!尤其是标点错或某个函数中了一个字符错,错不大,难找。* 代码及注释。 ~~~ <?php //导入smarty核心文件. require_once('config.php'); //定义变量 $title='这是测试标题'; $content='这是测试内容'; //将变量赋值模板中的变量 $smarty->assign('title',$title); $smarty->assign('content',$content); //调用模板文件. $smarty->display('index.html'); ?> ~~~ 2、在www/cart/templates中建立index.html. 代码如下: ~~~ <!doctype html> <html> <head> <meta charset="utf-8"> <!--模板变量title,它接收index.php中传过来的值--> <title>{$title}</title> <!--bootstrap.min.css样式链接--> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css"> </head> <body> <hr /> <!--模板变量content--> <h2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$content}</h2> <hr /> </body> </html> ~~~ bootstrap.min.css,用此样式表,使菜鸟也能编出优雅、大气的网页。使用网上的链接为了测试方便。 运行测试:http://localhost/cart/index.php 效果图: ![](https://box.kancloud.cn/9619a4d07ec09a99c1524309da6ff759_263x165.gif)   说明smarty模板配置成功,下一步该进行数据库连接文件设置了。 * * * * * ## 天行健,君子当自强不息! * * * * *