Smarty Simple Application Example _php instance

Source: Internet
Author: User
Tags smarty template
This paper describes a simple application example of Smarty. Share to everyone for your reference, as follows:

<?phprequire ' smarty/libs/smarty.class.php '; $smarty = new Smarty; $smarty->template_dir= "smarty/templates/ 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 MySQL server connection mysql_select_db ("lava_guess"); Link Database mysql_query ("Set names UTF8"); Solve 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, returns the result//converts the recordset to an array while ($rs _list=mysql_fetch_array ($result _list)) {$msg _array[]= $rs _list;} $array [] = Array ("NewsID" =>1, "newstitle" = "1th 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", $array); $smarty Assign ("Hello", $hello);//Assignment $smarty->assign ("Msglist", $msg _array); $smarty->display (' Index.tpl ');? >

Template file: Index.tpl

   Smarty{$hello} {section name=loop loop= $msglist} ID: {$msglist [loop].id}--{$msglist [Loop].title}----{$msglist [Loop].receiver}
{/section} {section name=loop loop= $newsArray} News Number: {$newsArray [Loop].newsid}
News title: {$newsArray [Loop].newstitle}
{Sectionelse} Sorry, there is no news input! {/section}

It is hoped that this article will be helpful to everyone based on smarty template PHP program design.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.