How to generate html in batches in Smarty

Source: Internet
Author: User
After some exploration, I finally found a method and wrote it out for you to give pointers: first in smartt. class. in php, add the following method to the Smarty class: PHP code: parameter 1: html file storage path, parameter 2: written content functionMakeHtmlFile ($ file_name, $ c) {& nbsp; & nbsp; if (! $ Fpfopen ($ file_name, after some exploration, I finally found a method. let's give you some advice:
First, add the following method to the Smarty class in smartt. class. php:
PHP code:
// Parameter 1: html file storage path; parameter 2: written content
Function MakeHtmlFile ($ file_name, $ c)
{
If (! $ Fp = fopen ($ file_name, "wa "))
{
Echo "file opening failed! ";
Return false;
}
If (! Fwrite ($ fp, $ c ))
{
Echo "file writing failed! ";
Fclose ($ fp );
Return false;
}
Fclose ($ fp );
}


Q: all my articles call the news. tpl template. how can I generate them in batches?

Let's take a look at news. php.
PHP code:

Include_once ("config. php ");
Include_once ("init. php ");
$ S-> assign ("title", "all news categories ");
$ ID = $ _ GET ["ID"] + 0;
$ SQL = "select * from artical where newsID = $ ID ";
$ Rs = $ db-> fetch ($ SQL );
$ S-> assign ("news", $ rs ["rec"] [0]); // Note: $ rs ["rec"] [0] is an array
$ S-> display ("news.html ");
?>


The entire template variable is only $ news. What are the advantages of this writing? I can read content in arrays.

Then how to generate:
See the following code:
Very simple
PHP code:

Include_once ("config. php ");
Include_once ("init. php ");
$ SQL = "select * from artical ";
$ Rs = $ db-> fetch ($ SQL );
Foreach ($ rs ["rec"] as $ k => $ v)
{
$ S-> assign ("news", $ v );
$ S-> MakeHtmlFile (". /news _". $ v [0]. ". html ", $ s-> fetch (" news.html ", null, null, false ));
}
?>


It's easy!


PHP code:




<{$ News. titles}>






















                   
<{$ News. titles}>
Author:<{$ News. author}>Time:<{$ News. sj}>Keywords:<{$ News. keyWord}>
Core tips:<{$ News. sumary}>
<{$ News. contents}>



Summary: The smarty fetch method and file read/write operations are mainly used.

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.