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 ); } ?> |