首页 > php教程 > php手册 > 正文

PHP(3): Start using Smarty

php中文网
发布: 2016-06-13 10:54:04
原创
953人浏览过

1. About Smarty

when we are doing web programming using php, one problem is that the php files can be mixed with php code as long as the html code. at some point, it is not very clean and also not safe. and the work can't be seperated for back-end programmers and front-end programmers. so we need a tool to seperate the php logic from the html code to well organize the and maintain the developing process.so here it comes smarty.

Smarty is a web template system written in PHP. Smarty is primarily promoted as a tool for separation of concerns.[1] Smarty is intended to simplify compartmentalization, allowing the presentation of a web page to change separately from the back-end. Ideally, this eases the costs and efforts associated with software maintenance.

 

立即学习PHP免费学习笔记(深入)”;

Smarty generates web content by the placement of special Smarty tagswithin a document. These tags are processed and substituted with other code. Tags are directives for Smarty that are enclosed by template delimiters. These directives can be variables, denoted by a dollar sign ($), functions, logical or loop statements. Smarty allows PHP programmers to define custom functions that can be accessed using Smarty tags.

 

立即学习PHP免费学习笔记(深入)”;

2. Set up Smarty

Step1: download the Smarty and rename it's libs folder and import it into our php project.

Step2: create a php file to connect to Smarty( SmartyCon.php)

[php] view plaincopyprint?

/* 

 * Created on Jan 10, 2013 

 * Author: Nick 

 * Function: Connecting to Smarty 

 */  

 include_once("smarty/Smarty.class.php");  

 $smarty = new Smarty();          //new an instance of smarty  

 $smarty->config_dir = "smarty/";      //smarty's config info  

 $smarty->caching = false;         //use cache or not  

 $smarty->template_dir = "./templates";   //set the folder for keeping the templates  

 /** 

  * smarty can automatically compile the templates and php contents to an mixed file 

  * and be stored in templates_C folder 

  */  

 $smarty->compile_dir = "./templates_c";  //the folder that store compiled files  

 $smarty->cache_dir = "./smarty_cache";   //store cache files  

  

 $smarty->left_delimiter = "{";  

 $smarty->right_delimiter = "}";  

  

 ?>  

 

立即学习PHP免费学习笔记(深入)”;

According to the code we should also create 3 folders which are used to store some corresponding files. templates folder is used to store html files which as the folder's name shows: they are templates, and will be called by "$smarty->display()" to show different styles for a project. tempates_c is used to store the compiled files. php files and templates are written in different files, but the php compiler can compile the templates and php contents to an mixed file and store them into templates_c folder. smarty_cache is used to store cache files.

 

立即学习PHP免费学习笔记(深入)”;

Step3: write the php content ( a.php)

[php] view plaincopyprint?

/* 

 * Created on Jan 10, 2013 

 * Author: Nick 

 * Function: 

 */  

 include("SmartyCon.php");  

 $name = "php100";  

 //$smarty->assign("title",$name); //assign php variabel to the tab in templates  

 //$smarty->display("a.html");    //show the template  

  

 $nameTwo[] = array("name"=>"jimmy","city"=>"Montreal");  

 $nameTwo[] = array("name"=>"tim","city"=>"wuxi");  

 $nameTwo[] = array("name"=>"sam","city"=>"newyork");  

 $nameTwo[] = array("name"=>"john","city"=>"sanfran");  

 $nameTwo[] = array("name"=>"lily","city"=>"loyola");  

  

 $title= array("a"=>"name","b"=>"News","c"=>"date","d"=>"now()");  

  

 $smarty->assign("title",$nameTwo); //assign php variabel to the tab in templates  

 $smarty->assign("ab",$title);  

  

 $smarty->display("a.html");  //show the template  

?>  

 

立即学习PHP免费学习笔记(深入)”;

 

立即学习PHP免费学习笔记(深入)”;

As the code abouve shows, we can use $smarty->assign("ab",$title), we can assign a php variable to a smarty variable. Then we use $smarty->display() to display the corresponding template. In the template file we havce to use the same assigned file to display the value of the php content here. For example, if we want the template show $title's "News". In a.html file we have to use "{$ab[b]}" 

 

立即学习PHP免费学习笔记(深入)”;

Step 4: write template file( a.html)

[html] view plaincopyprint?

 

{$ab["b"]}

 


 

{$title}  

  

{section name=list loop=$title}  

 

    {$title[list].name} - {$title[list].city}  


 

{/section}  

 

 

立即学习PHP免费学习笔记(深入)”;

 

立即学习PHP免费学习笔记(深入)”;

The result is like this:

As to how to print out the values in a two dimentional table, we have to use {section name='' loop=$..}{/section}

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号