ASP.NET 有没有类似PHP的Smarty这样的模板引擎

pasttender 2010-04-15 10:51:24
如题
...全文
350 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dbfoxmgr 2011-03-13
  • 打赏
  • 举报
回复
同时还提供了一个[D:y-m-d,DateTime] 的表达式 可以生成时间


<!--list:infolist|25|ID,Title,Description,WeekHits,DateTime|{$whereStr$}|

<li class="top">
<em>[i]</em>
<a href="/html/{ID}.html" title="{Description:16}">{Title}</a>
<span class="score">{WeekHits}</span>
<b>[D:y-m-d,DateTime]</b>
</li>

||WeekHits desc-->


dbfoxmgr 2011-03-13
  • 打赏
  • 举报
回复
我写了一个 简单的,但是不是 smarty
代码是这样的:
第一种:

<!--list:infolist|25|ID,Title,Description,WeekHits|{$whereStr$}|
3:
<li class="top">
<em>[i]</em>
<a href="/html/{ID}.html" title="{Description:16}">{Title}</a>
<span class="score">{WeekHits}</span>
</li>
@
22:
<li>
<em>[i]</em>
<a href="/html/{ID}.html" title="{Description:16}">{Title}</a>
<span class="score">{WeekHits}</span>
</li>
||WeekHits desc-->

第二种:

<!--list:infolist|25|ID,Title,Description,WeekHits|{$whereStr$}|
<li class="top">
<em>[i]</em>
<a href="/html/{ID}.html" title="{Description:16}">{Title}</a>
<span class="score">{WeekHits}</span>
</li>
||WeekHits desc-->





using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;


public class RegLi
{
public static string Execute(string content)
{
string temString = content;
Regex Reg = new Regex("<!--list:(?<res>[\\S\\s]+?)-->");

foreach (Match Match in Reg.Matches(content))
{
string temstr = Match.Groups["res"].ToString();
temString = temString.Replace("<!--list:" + temstr + "-->", QueryDB(temstr));

}

return temString;
}

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

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

string htm = ParamesArr[4].Trim();

//找出 格式化字符
ArrayList Al = new ArrayList();
if (htm.IndexOf("@") > 0)
{
string[] SArr = htm.Split('@');
foreach (string s in SArr)
{

string[] ZArr = Regex.Split(s, ":\r\n");
Al.Add(new string[] { ZArr[0], ZArr[1] });

}
}
else
{

Al.Add(new string[] { "100", htm });
}


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



//查找出所有的字段

Regex Reg = new Regex("{(?<fd>[a-zA-Z0-9:_]+?)}");

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])));
}


ContentStr.Append(temHtm);

}
else
{
isEof = true;
}


}

}
sdr.Close();
sdr.Dispose();

temString = ContentStr.ToString();

}
else
{
temString = "";
}



return temString;
}

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;
}
}



masky5310 2010-04-15
  • 打赏
  • 举报
回复
Php不懂
帮顶个
ddhd521 2010-04-15
  • 打赏
  • 举报
回复
帮顶,我也求
rosejing 2010-04-15
  • 打赏
  • 举报
回复
帮顶, 跪求
parverxiao 2010-04-15
  • 打赏
  • 举报
回复
好像没有发现呢

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧