Smarty simple application instance and smarty application instance. Smarty simple application instance. smarty application instance this article describes the smarty simple application instance. For your reference, see phprequiresmartylibsSmarty. class. php; $ smarty simple application instance and smarty application instance.
This article describes a simple application example of smarty. 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.
This article describes the smarty simple application example. For your reference, see phprequire 'smarty/libs/smarty. class. php'; $...