示例#1
0
 public void TagEndProcess(TemplateManager manager, Tag tag, string innerContent)
 {
     Expression exp;
     string _len, _cutstring;
     exp = tag.AttributeValue("len");
     if (exp == null)
         throw new TemplateRuntimeException("cut标签必须有 len 属性.", tag.Line, tag.Col);
     _len = manager.EvalExpression(exp).ToString();
     //对内容进行裁剪并添加省略号
     _cutstring = GetSummary(innerContent, int.Parse(_len));
     manager.WriteValue(_cutstring);
 }
示例#2
0
        public void TagEndProcess(TemplateManager manager, Tag tag, string innerContent)
        {
            Expression exp;
            string strFile, strResult;
            string strSource = "", strCharset = "";
            //获取文件路径参数
            exp = tag.AttributeValue("file");
            if (exp == null)
                throw new TemplateRuntimeException("include 标签必须有 file 属性.", tag.Line, tag.Col);
            strFile = manager.EvalExpression(exp).ToString();
            //获取文件来源参数,file-文件系统,web-远程网页
            exp = tag.AttributeValue("source");
            if (exp == null)
                strSource = "file";
            strSource = manager.EvalExpression(exp).ToString();
            //获取字符集
            exp = tag.AttributeValue("charset");
            if (exp == null)
                strSource = "utf-8";
            strCharset = manager.EvalExpression(exp).ToString();

            //读取文件内容并插入到模板中
            if (strSource == "file")
            {
                //读取本地文件内容
                if (System.IO.File.Exists(strFile))
                    strResult = System.IO.File.ReadAllText(strFile);
                else
                    strResult = string.Format("[{0}文件不存在.]", strFile);
            }
            else
            {
                //抓取远程网页内容
                string strSiteUrl = string.Format("{0}://{1}", System.Web.HttpContext.Current.Request.Url.Scheme, System.Web.HttpContext.Current.Request.Url.Authority);
                strFile = strFile.Replace("~", strSiteUrl);
                WebClient MyWebClient = new WebClient();
                MyWebClient.Credentials = CredentialCache.DefaultCredentials;
                MyWebClient.Encoding = Encoding.GetEncoding(strCharset);
                strResult = MyWebClient.DownloadString(strFile);
            }
            manager.WriteValue(strResult);
        }