首页 新闻 搜索 专区 学院

有能实现标签化的模板引擎吗?

0
[待解决问题]

像动易的标签,比如{PE.DataSource id="cone" datasource="图片栏目循环列表信息" /}

有朋友向我推荐了razor和Nvelocity,可是似乎这些模板引擎在前台还要出现if else等等的控制语句,不能简单到仅仅一个标签的程度

请问大家,有没有像动易那样,把所有前台除了最基本的html标签之外的内容都表示成一个标签的方法?不要任何的控制语句出现,全部标签也只是{PE.DataSource id="cone" datasource="图片栏目循环列表信息" /}这种形式,不要搞成多行那么麻烦

只有这样,前台的人员才可以真正做到不需要后台的任何技术要求

有没有这样的模板引擎呢?

另外还有一个问题是:在mvc和webform两种架构方式上都能使用模板引擎吗?有什么区别吗?

nbh002的主页 nbh002 | 初学一级 | 园豆:200
提问于:2011-05-26 12:15
< >
分享
所有回答(2)
0

哈哈哈哈,我写了一个

方式一:

<!--li:SoftDown_SoftInfo|10|SoftName,jianjie,SN,path,domain,prefix,Replace(Round(score/scoreNum,1),'00000000000','') as score,images|CatalogID={$CatalogID$} or CatalogID in(Select CatalogID from [SoftDown_Catalog] where ParentID={$CatalogID$})|
1:
<dt>
<a href="{domain}{path}{prefix}{SN}.html" class="img">
<img src="{images}" />
</a>
<div class="inf">
<a class="title" href="{domain}{path}{prefix}{SN}.html">{SoftName}</a>
<p>{jianjie}<a href="{domain}{path}{prefix}{SN}.html">[详情]</a></p>
<span>评分:<em>{score}</em></span>
</div>
</dt>
@
9:
<dd>
<span>{score}</span>
<em>[i].</em><a href="{domain}{path}{prefix}{SN}.html">{SoftName:16}</a>
<p>{jianjie:20}</p>
</dd>
|no Resault
|MonthHits desc
-->

方式2:

 

<!--li:SoftDown_SoftInfo|10|SoftName,jianjie,SN,path,domain,prefix,Replace(Round(score/scoreNum,1),'00000000000','') as score,images|CatalogID={$CatalogID$} or CatalogID in(Select CatalogID from [SoftDown_Catalog] where ParentID={$CatalogID$})|
<dd>
<span>{score}</span>
<em>[i].</em><a href="{domain}{path}{prefix}{SN}.html">{SoftName:16}</a>
<p>{jianjie:20}</p>
</dd>
|no Resault
|MonthHits desc
-->

 

提供 [i] 变量 用于 输出 1,2,3,4,5,6

提供变量[D:y-m-d,日期字段名] 用于 输出格式化的日期

 

代码如下:

调用:

string phtml=list.Execute(templateString);

源代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
/// <summary>
///list 的摘要说明
/// </summary>
public class list
{
public list()
{
//
//TODO: 在此处添加构造函数逻辑
//
}

public static string Execute(string strContent)
{
string returnString = strContent;
if (strContent==null)
{
returnString
= "";
}
else
{

Regex Reg
= new Regex("<!--list:(?<params>[\\S\\s]+?)-->");
MatchCollection matches
= Reg.Matches(strContent);
foreach (Match match in matches)
{
string Params = match.Groups["params"].ToString();

returnString
= returnString.Replace(@"<!--list:" + Params + "-->", GET(Params));

}
}
return returnString;
}

private static string GET(string Params)
{
StringBuilder retrunSB
= new StringBuilder();
string[] ParamsArr = Params.Split('|');
if (ParamsArr.Length==7)
{
string tbn = ParamsArr[0].Trim();//表名
string top = ParamsArr[1].Trim();//select top n
if (top != "")
{
top
= " top " + top;
}
string fls = ParamsArr[2].Trim();//字段列表 以"," 分割
string flsStr = " ";
if (fls == "")
{
flsStr
= @" * ";
}
else
{
flsStr
= " " + fls + " ";
}

string whe = ParamsArr[3].Trim();
if (whe != "")
{
whe
= " where " + whe;
}
string htm = ParamsArr[4].Trim();

//找出 格式化字符
ArrayList Al = new ArrayList();
if (htm.IndexOf("@") > 0)
{
string[] SArr = htm.Split('@');
foreach (string s in SArr)
{
string[] ZArr = Regex.Split(s, ":[\\s^\\n]*\\n");
Al.Add(
new string[] { ZArr[0], ZArr[1] });
}
}
else
{
Al.Add(
new string[] { "100", htm });
}

string nor = ParamsArr[5].Trim();//当没有记录时候显示的内容
string ord = ParamsArr[6].Trim();//排序
if (ord != "")
{
ord
= " order by " + ord;
}

//查找出所有的字段
Regex Reg = new Regex("{(?<fd>[\\w:]+?)}");

Dictionary
<string, string> Dict = new Dictionary<string, string>();
foreach (Match M in Reg.Matches(htm))
{
string Ms = M.Groups["fd"].ToString();


string[] SArr = Ms.Split(':');
if (!Dict.ContainsKey(Ms))
{
if (Ms.IndexOf(":") > 0)
{
Dict.Add(Ms, Ms);
}
else
{
Dict.Add(Ms, SArr[
0] + ":0");
}
}
}

//[D:y-m-d h-n-s,Datetime]
string dateStr = "";
bool isHaveDateFiled = false;
string parseDateForma = "";
string parseDateFiled = "";

Regex tregExg
= new Regex("\\[D:(?<res>[\\S\\s]*?)\\]");
MatchCollection Matchs
= tregExg.Matches(htm);
if (Matchs.Count == 1)
{
foreach (Match match in Matchs)
{
string Mstr = match.Groups["res"].ToString();
dateStr
= "[D:" + Mstr + "]";
string[] tArr = Mstr.Split(',');
parseDateForma
= tArr[0];
parseDateFiled
= tArr[1];

}
isHaveDateFiled
= true;
}

string sql = "select " + top + flsStr + " from " + tbn + whe + ord;

SqlDataReader sdr
= db.ExecuteReader(sql);
int inx = 0;
bool isEof = false;

for (int TypeRowInx = 0; TypeRowInx < Al.Count; TypeRowInx++)
{
if (isEof) break;
string[] RArr = (string[])Al[TypeRowInx];

for (int RowInx = 0; RowInx < Convert.ToInt32(RArr[0]); RowInx++)
{
if (sdr.Read())
{
inx
++;
string temHtm = RArr[1];
temHtm
= temHtm.Replace("[i]", inx.ToString());
if (isHaveDateFiled)
{
DateTime tDat
= (DateTime)sdr[parseDateFiled];
string st = parseDateForma;
st
= st.Replace("y", tDat.Year.ToString());
st
= st.Replace("m", tDat.Month.ToString());
st
= st.Replace("d", tDat.Day.ToString());
st
= st.Replace("h", tDat.Hour.ToString());
st
= st.Replace("n", tDat.Minute.ToString());
st
= st.Replace("s", tDat.Second.ToString());
//DateTime.Now.
if (DateTime.Now.Year == tDat.Year && DateTime.Now.Month == tDat.Month && DateTime.Now.Day == tDat.Day)
{
st
= "<b class=\"new\">" + st + "</b>";
}
else
{
st
= "<b class=\"old\">" + st + "</b>";
}
temHtm
= temHtm.Replace(dateStr, st);
}
//替换字段信息
foreach (KeyValuePair<string, string> kvp in Dict)
{
string[] tary = kvp.Value.ToString().Split(':');
temHtm
= temHtm.Replace("{" + kvp.Key + "}", CutStr(sdr[tary[0]].ToString(), Convert.ToInt32(tary[1])));
}
retrunSB.Append(temHtm);
}
else
{
isEof
= true;
}
}
}
sdr.Close();
sdr.Dispose();
}
return retrunSB.ToString();
}

private static string CutStr(string temString, int length)
{
string str = temString;
if (length > 0)
{
if (temString.Length > length)
{
str
= str.Substring(0, length);
}
}
return str;
}

}

dbfox | 园豆:2 (初学一级) | 2011-05-26 13:18
0

nvelocity 你可以基于其再封装自己的标签

码尔代夫iimax | 园豆:3138 (老鸟四级) | 2011-05-27 11:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册