This article mainly introduces the simple application example of smarty, and briefly introduces the skills related to database query, array traversal, and data display by calling templates in smarty, which is easy to understand and has reference value, for more information, see the smarty simple application example. We will share this with you for your reference. The details are as follows:
<? Phprequire 'smarty/libs/smarty. class. php '; $ smarty = new Smarty; $ smarty-> template_dir = "smarty/templates"; $ smarty-> compile_dir = "smarty/templates/templates_c "; $ smarty-> config_dir = "smarty/templates/config"; $ smarty-> cache_dir = "smarty/templates/cache"; $ hello = "Hello World! "; $ Smarty-> compile_check = true; // $ smarty-> debugging = true; $ smarty-> debugging = false; $ smarty-> caching = true; $ conn = mysql_connect ("localhost", "root", "root"); // open the MySQL server to connect to mysql_select_db ("lava_guess "); // link to the database mysql_query ("set names utf8"); // solve the Chinese garbled problem $ SQL _list = "Select * from t_sys_msg Order by id desc limit 0, 10 "; $ result_list = mysql_query ($ SQL _list); // executes the SQL statement and returns the result. // converts the record set to an array while ($ rs_list = mysql_fet Ch_array ($ result_list) {$ msg_array [] = $ rs_list;} $ array [] = array ("newsID" => 1, "newsTitle" => "1st News"); $ array [] = array ("newsID" => 2, "newsTitle" => "2nd News "); $ array [] = array ("newsID" => 3, "newsTitle" => "3rd News"); $ array [] = array ("newsID" => 4, "newsTitle" => "4th News"); $ array [] = array ("newsID" => 5, "newsTitle" => "5th News "); $ array [] = array ("newsID" => 6, "newsTitle" => "6th News"); $ smarty-> assign ("newsArray", $ arra Y); $ smarty-> assign ("hello", $ hello); // value $ smarty-> assign ("msglist", $ msg_array ); $ smarty-> display ('index. tpl ');?>
Template File: index. tpl
Smarty{$ Hello} {section name = loop = $ msglist} id: {$ msglist [loop]. id} -- {$ msglist [loop]. title} ---- {$ msglist [loop]. else er}
{/Section} {section name = loop = $ newsArray} News No.: {$ newsArray [loop]. newsID}
News Title: {$ newsArray [loop]. newsTitle}
{Sectionelse} Sorry, no news input! {/Section}
I hope this article will help you design php programs based on the smarty template.