General methods for dynamically generating static html pages

Source: Internet
Author: User
General methods for dynamically generating static html pages
/** // <Summary>
/// Dynamically generate an HTML page -- Korea International)
/// </Summary>
/// <Param name = "format"> ** Replace the variable of the HTML template ($ htmlformat [*]), the first variable format [0] in the format array should be the relative path of the generated static html page to the image directory images, such :".. /"</param>
/// <Param name = "templateurl"> call the URL + Template Name of the HTML template, for example, "Temp/template.htm" </param>
/// <Param name = "savehtmlurl"> Save the generated HTML page url + HTML file name, for example, "Temp/20060418.htm" </param>
/// <Param name = "encodingname"> the encoding method used to read the HTML template page and generate the HTML page, for example, "gb2312" </param>
/// <Returns> boolean type -- true = generation succeeded, false = generation failed </returns>
Public bool writehtml (string [] format, string templateurl, string savehtmlurl, string encodingname)
{
// --------------------- Read the HTML template page to the stringbuilder object ----
System. Text. stringbuilder htmltext = new system. Text. stringbuilder ();
Try
{
System. Io. streamreader sr = new system. Io. streamreader (server. mappath (templateurl. Trim (), system. Text. encoding. getencoding (encodingname ));
String line;
While (line = Sr. Readline ())! = NULL)
{
Htmltext. append (line );
}
Sr. Close ();
}
Catch
{
System. Web. httpcontext. Current. response. Write ("<SCRIPT> alert ('read HTML template" + templateurl. Trim () + "error! '); </SCRIPT> ");
}
// ---------- Replace the mark in the HTM template with the passed Array
For (INT I = 0; I <format. length; I ++)
{
Htmltext. Replace ("$ htmlformat [" + I + "]", format [I]);
}
// ---------- Generate the HTM file ------------------――
Try
{
String tmphtm = server. mappath (savehtmlurl );
System. Io. streamwriter Sw = new system. Io. streamwriter (tmphtm. Trim (), false, system. Text. encoding. getencoding (encodingname ));
Sw. writeline (htmltext );
Sw. Flush ();
Sw. Close ();
Return true;
}
Catch
{
System. Web. httpcontext. Current. response. Write ("<SCRIPT> alert ('" + savehtmlurl. Trim () + "HTML file generation failed! '); </SCRIPT> ");
Return false;
}
}

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.