时间:2021-07-01 10:21:17 帮助过:14人阅读
Step2: create a php file to connect to Smarty( SmartyCon.php)
[php] view plaincopyprint?
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 = "}";
?>
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.
Step3: write the php content ( a.php)
[php] view plaincopyprint?
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
?>
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]}"
Step 4: write template file( a.html)
[html] view plaincopyprint?
As to how to print out the values in a two dimentional table, we have to use {section name='' loop=$..}{/section}
http://www.bkjia.com/PHPjc/477816.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477816.htmlTechArticle1. 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 cle...