模板页面

<!--滚动头条templateStart-->
<href="<!--滚动头条link-->" target="_blank"><font color="red"><!--滚动头条title--></font></a>
<href="<!--滚动头条link-->" target="_blank"><!--滚动头条title--></a>  
滚动头条用ajax异步实现
    [Ajax.AjaxMethod()]
        public string PubHeadLines()
        {
            
//滚动头条
            DataTable dt = dp.GetPageContent("T","headLines",-1,50);
            
string block ="";
            
bool f = true;
            
int index = 0;
            block 
= TemplateManager.LoadTemplateHtml(TemplateManager.FRONTPAGE,"滚动头条");    
            
string showName = "";
            
string url = "",state="";
            
while(f&&dt!=null&&dt.Rows.Count>index)
            {        
                showName 
= dt.Rows[index]["ShowName"].ToString();
                
if(showName.Length < 1)
                    showName 
= dt.Rows[index]["Title"].ToString();
                url 
= dt.Rows[index]["url"].ToString();
                state 
= dt.Rows[index]["State"].ToString();
                f 
= TemplateManager.ReplaceTemplateContent("滚动头条",ref block,showName,url,"",state);                
                index
++;
            }
            
return block;
        }

TemplateManager.cs类

 Components
{

    /// <summary>
    
/// TemplateManager 的摘要说明。
    
/// </summary>
    public class TemplateManager
    {
        
//各超市首页模版
        public static readonly string FRONTPAGE=System.Web.HttpContext.Current.Server.MapPath("~/template/FrontPage.html");
        
public static readonly string shoppingCar=System.Web.HttpContext.Current.Server.MapPath("~/EmailModel/index.html");
        
        
/// <summary>
        
/// 加载模版代码
        
/// </summary>
        
/// <param name="tempPath">模版路径</param>
        
/// <param name="blockTitle">
        
/// 标题起始
        
/// 如<!--银行产品快讯templateStart-->
        
/// 则传 blockTitle=银行产品快讯
        
/// </param>
        
/// <returns></returns>
        public static string LoadTemplateHtml(string tempPath,string blockTitle)
        {
            
string html = "",result="";
            
using(StreamReader sr = new StreamReader(tempPath,Encoding.GetEncoding("utf-8")))
            {
                html 
= sr.ReadToEnd().ToLower();
                sr.Close();
            }
            
if(blockTitle=="")
            {
                
return html;
            }
            
string beginTag = "<!--"+blockTitle+"templatestart-->";
            
string endTag = "<!--"+blockTitle+"templateend-->";
            
int beginIndex = html.IndexOf(beginTag);
            
int endIndex = html.IndexOf(endTag);
            
if( beginIndex != -1 && endIndex > beginIndex)
            {
                result 
= html.Substring(beginIndex+beginTag.Length,endIndex-beginIndex-beginTag.Length);
            }
            
return result;
        }

        
/// <summary>
        
/// 替换模版内容
        
/// 每次替换一条
        
/// </summary>
        
/// <param name="tagName">标题名称 如:银行理财产品快讯</param>
        
/// <param name="blockHtml">模版块HTML</param>
        
/// <param name="title">链接标题</param>
        
/// <param name="url">链接地址</param>
        
/// <param name="date">发布日期</param>
        
/// <param name="state">0默认普通,1new,2hot,12hot and new</param>
        
/// <returns>成功 返回 true 失败 返回 False</returns>
        public static bool ReplaceTemplateContent(string tagName,ref string blockHtml,string title,string url,string date,string state)
        {
            
string init = blockHtml;
            
            
string tagLink = "<!--"+tagName+"link-->";//链接
            string tagTitle = "<!--"+tagName+"title-->";//标题
            string tagDate = "<!--"+tagName+"date-->";//日期
            
            
if(state.IndexOf("1")!=-1)
            {                
                title 
+= "&nbsp;<img src='http://www.cnqsq.com/images/ico_new.gif' border=0/>";
            }
            
if(state.IndexOf("2")!=-1)
            {
                title 
+= "&nbsp;<img src='http://www.cnqsq.com/images/ico_hot.gif' border=0/>";
            } 

            
//链接
            blockHtml = replaceOneTag(tagLink,url,blockHtml);
            
//标题
            blockHtml = replaceOneTag(tagTitle,title,blockHtml);
            
//日期
            blockHtml = replaceOneTag(tagDate,date,blockHtml);

            
return blockHtml!=init;
        }
        
public static bool ReplaceShoppingCarTemplate(string tagName,ref string blockHtml,string title,string url,string content,string state)
        {
            
string init = blockHtml;
            
string tagContent ="<!--"+tagName+"content-->";
            
string tagTitle ="<!--"+tagName+"title-->";
            
string tagUrl ="<!--"+tagName+"url-->";
            
            
if(state.IndexOf("1")!=-1)
            {                
                title 
+= "&nbsp;<img src='http://www.cnqsq.com/images/ico_new.gif' border=0/>";
            }
            
if(state.IndexOf("2")!=-1)
            {
                title 
+= "&nbsp;<img src='http://www.cnqsq.com/images/ico_hot.gif' border=0/>";
            } 

            blockHtml 
= replaceOneTag(tagUrl,url,blockHtml);
            blockHtml 
= replaceOneTag(tagUrl,url,blockHtml);
            blockHtml 
= replaceOneTag(tagTitle,title,blockHtml);
            blockHtml 
= replaceOneTag(tagContent,content,blockHtml);
            
return blockHtml!=init;
        }

     

        
/// <summary>
        
/// 替换指定代码中一个标识的内容
        
/// </summary>
        
/// <param name="tag">标识名 全称 如:<!--银行理财产品快讯link--></param>
        
/// <param name="replace">替换的内容</param>
        
/// <param name="html">代码</param>
        
/// <returns></returns>
        public static string replaceOneTag(string tag,string replace,string html)
        {
            
if(html.IndexOf(tag)!=-1)
            {
                
string temp = html.Substring(0,html.IndexOf(tag)+tag.Length);
                
string tempEnd = html.Substring(html.IndexOf(tag)+tag.Length);

                
string s = "";
                
if(tag.IndexOf("title")!=-1&& replace.IndexOf("<img")!=-1)
                {
                    
//带图标
                    s = replace.Substring(replace.IndexOf("<img"));
                    replace 
= replace.Substring(0,replace.IndexOf("<img"));
                }
                
if(tempEnd.TrimStart().IndexOf("<!--max")==0)
                {
                    
//限定长度
                    
                    
int maxLen = Tools.TypeParse.StrToInt(tempEnd.Substring(tempEnd.IndexOf("<!--max:")+"<!--max:".Length,tempEnd.IndexOf("-->")-tempEnd.IndexOf("<!--max:")-"<!--max:".Length).Trim().Replace(" ",""),-1);
                    
if(maxLen!=-1)                    
                        replace 
= Formater.GetSubString(replace,maxLen,"");
                    
//移除max
                    
                }
                replace 
= replace + s;
                html 
= temp.Replace(tag,replace)+html.Substring(html.IndexOf(tag)+tag.Length);
            }
            
return html;
        }
    }
}

相关文章: