Smarty basics

Source: Internet
Author: User
Tags php file

Original php file code:
<? Php
Include_once ("./comm/Smarty. class. php"); // contains the smarty class file
Define ("NUM", 5); // defines the number of news entries displayed each time.
$ Smarty = new Smarty (); // Create a smarty instance object $ smarty
$ Smarty-> templates_dir = "./templates"; // set the template directory
$ Smarty-> compile_dir = "./templates_c"; // Set the compilation Directory
$ Smarty-> cache_dir = "./cache"; // you can specify a cache directory.
$ Smarty-> cache_lifetime = 60*60*24; // you can specify the cache time.
$ Smarty-> caching = false; // set this parameter to false during debugging. Use true for release.
$ Smarty-> left_delimiter = "<{"; // you can specify the left boundary.
$ Smarty-> right_delimiter = "}>"; // you can specify the right boundary.
$ Db = mysql_connect ('localhost', 'root', '123 ');
Mysql_select_db ('Liu ', $ db );
$ SQL = "select id, title from lyb ";
Mysql_query ("set names gb2312 ");
$ Result = mysql_query ($ SQL, $ db );
While ($ row = mysql_fetch_array ($ result )){
$ Array [] = array ("id" => $ row ['id'],
"Title" => $ row ['title']);

}
Mysql_close ();
$ Smarty-> assign ('new', $ array );
$ Smarty-> display('3.html ');
?>
3. html
<Table width = "100%" border = "0" cellpadding = "0" cellspacing = "0" bgcolor = "# B9E9FF">
<Tr>
<Td height = "115" width = "10"> </td>
<Td valign = "top" width = "295" bgcolor = "# B9E9FF">
<{Section name = loop = $ new}>
<{$ New [loop]. id}>
<{$ New [loop]. title}> <br>
<{/Section}>
</Td>
</Tr>
</Table>
Now let's explain: Things in 3. php.
Let's take a look at the process writing!
$ Smarty = new Smarty (); // Create a smarty instance object $ smarty
$ Smarty-> templates_dir = "./templates"; // set the template directory. This must exist (the HTML file is here)
$ Smarty-> compile_dir = "./templates_c"; // Set the compiling directory to be real (compiled directory)

$ Smarty-> cache_dir = "./cache"; // you can specify a cache directory.
$ Smarty-> cache_lifetime = 60*60*24; // you can specify the cache time.
$ Smarty-> caching = false; // set this parameter to false during debugging. Use true for release.
$ Smarty-> left_delimiter = "<{"; // you can specify the left boundary.
$ Smarty-> right_delimiter = "}>"; // you can specify the right boundary.
$ Smarty-> assign ('new', $ array); // This is the section LOOP value of new in html.
$ Smarty-> display('3.html '); // which HTML file is this.
In html,. <{section name = loop = $ new}>
<{$ New [loop]. id}>
<{$ New [loop]. title}> <br>
<{/Section}>

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.