Pnuts CC's Blog Flower & World, Life & Paradise.

6Dec/095

Html Liked C# : Write HTML into C#

cshapelogo

Html-Liked-C#是自己写着玩的一个C#类库,故名思议,他提供一种可以像正常写html一样的方式在C#代码中生成html代码。而且由C#本身实现,不须预处理。效率方面,编译执行,应该有着和asp.net相当的效率。

本类库纯粹为个人爱好,源代码开放。请勿将其用作商业用途。另外,本人不对代码的安全性做出任何保障,使用者请自己承担风险。如有疑问欢迎和我联系

asp.net提供了一种很好的方式,使得html和c#可以交互书写(就是*.aspx啦),同时提供强大的控件功能(就是*.ascs啦)。不过呢,它还真不是万能的,有的时候你还真的自己用代码输出html。比方想要把一些asp.net的控件放到一个非web项目中。虽然可以先在web项目中写好,然后把编译器编译ascx生成的文件贴过去,但实在太麻烦了,而且可读和可修改性相当低。多数人都会选择下面两种方式:

所要生成的Html:

<div name="content">
	This is Content ...
</div>

第一种,直接写字符串:

void Render(HtmlTextWriter writer){
	writer.Write("<div name=\"content\">");
	wruter.Write(Content);
	writer.Write("</div>");
}

第二种,用HtmlTextWriter

void Render(HtmlTextWriter writer){
	writer.AddAttribute(HtmlTextWriterAttribute.Name, "Content");
	writer.RenderBeginTag(HtmlTextWriterTag.Div);
	writer.Write(Content);
	writer.RenderEndTag();
}

无论哪种,可读性都很低,而且很难修改,Html-Liked-C#就是绐这个问题另一个解决方案。它的写法如下:

void Render(HtmlTextWriter writer){
	writer.Write(_
		<div [name = "content"]
			<Content>_
		>_
	);
}

在来看另一个例子吧:

目标HTML如下:

<html>
	<head>
		<title>Html-Liked-C#</title>
	</head>
	<body>
		<h1 style="color:DarkBlue">Html-Liked-C#</h1>
		<ul>
			<li id="ins">
				<h2>Introduce</h2>
				<p>Html-Liked-C# provide a way to write html directly into c# code.</p>
			</li>
			<li id="stat">
				<h2>A table</h2>
				<table class="num" border="0" style="width:300px;border-style:dotted;">
				  <thead align=left>
					<tr>
					  <th>Month</th>
					  <th>Savings</th>
					</tr>
				  </thead>
				  <tfoot>
					<tr>
					  <td>Sum</td>
					  <td>$180</td>
					</tr>
				  </tfoot>
				  <tbody>
					<tr>
					  <td>January</td>
					  <td>$100</td>
					</tr>
					<tr>
					  <td>February</td>
					  <td>$80</td>
					</tr>
				  </tbody>
				</table>
			</li>
			<li id="about">
				<h2>About</h2>
				<a href="mailto:i@pnuts.cc">Contact Me.</a>
			</li>
		</ul>
	</body>
</html>

Html-Liked-C#:

class Program : HtmlGenerator
{
    static void Main(string[] args)
    {
        string[] MonName = new string[] { "January", "February" };
        int[] Money = new int[] { 100, 80 };
        Console.Write(html
            < head
                < title
                    < "Html-Liked-C#" > _
                > _
            > _
            < body
                < h1[style[color = darkblue]]
                    < "Html-Liked-C#" > _
                > _
                < ul
                    < li[id = "ins"]
                        < h2 < "Introduce" > _ > _
                        < p
                            < "Html-Liked-C# provide a way to write html directly into c# code." > _
                        > _
                    > _
                    < li[id = "stat"]
                        < h2 < "A Table" > _ > _
                        < table[@class = "num", border = 0, style[width = "300px", border_style = dotted]]
                            < thead[align = left]
                                < tr
                                    < th < "Month" > _ > _
                                    < th < "Savings" > _ > _
                                > _
                            > _
                            < tfoot
                                < tr
                                    < td < "Sum" > _ > _
                                    < td
                                        < (v =>
                                        {
                                            int sum = 0;
                                            foreach (var i in Money) { sum += i; }
                                            var s = v < sum;
                                        }) > _
                                    > _
                                > _
                            > _
                            < tbody
                                < (v =>
                                {
                                    for (int i = 0; i < Money.Length; i++)
                                    {
                                        var s = v
                                            < tr
                                                < td < MonName[i] > _ > _
                                                < td < Money[i] > _ > _
                                            > _;
                                    }
                                }) > _
                            > _
                        > _
                    > _
                    < li[id = "about"]
                        < h2 < "About" > _ > _
                        < a[href = "mailto:pnuts.cc"] < "Contact me." > _ > _
                    > _
                > _
            > _
        > _);
    }
}

注意哦,上面的C#应用的是html的配色方案,还可以吧?

关于具体的语法以及实现细节,明天再写吧,太累了。

转载注明出处:http://pnuts.cc/2009/12/html-liked-cshape/

Tagged as: , , Leave a comment
Comments (5) Trackbacks (0)
  1. Great website. Lots of useful information here. I’m sending it to several pals ans additionally sharing in delicious. And certainly, thanks on your effort!

  2. Oh my goodness! Impressive article dude! Thank you,
    However I am going through issues with your RSS.

    I don’t understand why I cannot subscribe to it. Is there anyone else having similar RSS problems? Anyone that knows the solution can you kindly respond? Thanx!!

  3. Thank you for finally writing about >Html Liked C# : Write HTML into C# Pnuts CC’s
    Blog <Loved it!

  4. Right here is the perfect website for everyone who wishes
    to find out about this topic. You know a whole lot its almost hard to argue with you (not
    that I really will need toHaHa). You certainly put a new spin on a
    topic that’s been discussed for ages. Excellent stuff, just wonderful!


Leave a comment

No trackbacks yet.