Smarty simple application instance, smarty application instance _ PHP Tutorial

Source: Internet
Author: User
Tags smarty template
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'; $...

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: info-contact@alibabacloud.com 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.