Php smarty template to generate static html page code

Source: Internet
Author: User
Tags mysql tutorial php file smarty template
The code is as follows: Copy code

Include ('./www.111cn.net/smarty/smarty.class.php ');
$ Smarty = new smarty ();
$ Smarty-> template_dir = "templates /";
$ Smarty-> compile_dir = "templates_c /";
$ Smarty-> left_delimiter = "<{";
$ Smarty-> right_delimiter = "}> ";

$ Db = mysql tutorial _ connect ('localhost', 'root ','');
Mysql_select_db ('test ');
Mysql_query ('set names "utf8 "');
$ Result = mysql_query ('select * from news ');
While ($ row [] = mysql_fetch_array ($ result )){
$ Smarty-> assign ('news', $ row );
}
// $ Smarty-> display('smarty_html.html ');
// You can understand the above without writing comments
// We will not show him here
// Get him
$ Content = $ smarty-> fetch('smarty_html.html ');
// Obtain the template file content of the smarty replacement all, that is, the content of smarty_html.php after the display
/*
In fact, this step is equivalent
Ob_start (); enable buffer
$ Smarty-> display('smarty_html.html ');
$ Content = ob_get_contents (); obtain the buffer content
Ob_end_claen; disable the buffer.
*/
Makehtml('news.html ', $ content); // write to the news.html file
Echo '<a href = "news.html"> View </a> ';
// Click to view the static page and the page is generated successfully!
// But there is a problem that the current page must be used.
// For example, in another php file, $ smarty-> fetch('smarty_html.html ');
// Only the content of the original template file can be obtained. Because there is no assign, all template variables are not replaced.
// Fee is also generated!
Function makehtml ($ file, $ content ){
$ Fp = fopen ($ file, 'w ');
Fwrite ($ fp, $ content );
Fclose ($ fp );
}
?>

// Template page

The code is as follows: Copy code
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = utf-8"/>
<Title> Untitled Document </title>
</Head>
<Body>
<{Section name = n loop = $ news}>
Id <{$ news [n]. news_id}> <br/>
Title <{$ news [n]. news_title}> <br/>
Content <{$ news [n]. news_content}>
<Hr/>
<{Sectionelse}>
No news
<{/Section}>
</Body>
</Html>

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.