![知乎周源微信](https://i-blog.csdnimg.cn/blog_migrate/684d70b4bff5a7f389db8032b8d9d05f.png)
知乎周源微信
I've been getting more and more interested in how folks extend their applications using plugins and things. In my new ongoing quest to read source code to be a better developer, Dear Reader, I present to you thirtieth in a infinite number of posts of "The Weekly Source Code."
我对人们如何使用插件和事物扩展其应用程序越来越感兴趣。 在我不断努力阅读源代码以成为更好的开发人员的过程中,亲爱的读者,我在“每周源代码”的无数篇文章中向您介绍了三十次。
火花 (Spark)
I'm really enjoying the extensibility points in ASP.NET MVC, but not as much as some people. Spark is a really promising ViewEngine from Louis DeJardin. You can download Spark here and join the mailing list here. Not only is it promising, it's also freaky. Freaky in that good, makes-you-think way.
我真的很喜欢ASP.NET MVC中的可扩展性点,但是却不如某些人那样。 Spark是Louis DeJardin的一个非常有前途的ViewEngine 。 您可以在此处下载Spark,并在此处加入邮件列表。 它不仅很有前途,而且还怪异。 以那种让您思考的好方法来抓狂。
Louis says "The idea is to allow the html to dominate the flow and the code to fit seamlessly." When he says HTML should dominate the flow, he's not kidding. Check this out:
Louis说:“这个想法是让html主导流程,并使代码无缝地契合。 ”当他说HTML应当主导流程时,他不是在开玩笑。 看一下这个:
<ul>
<li each='var p in ViewData.Model.Products'>
$p.Name; $Html.ActionLink[[ProductController]](c=>c.Edit(p.Id), "Edit");
</li>
</ul>
See the "each" attribute on the UL? Freakly. ;)
在UL上看到“每个”属性? 怪异的;)
Remember this is NOT WebForms. That <li> and the each attribute are not controls, or parts of controls or runat=server or whatever. This is a complete ViewEngine. It's a templating language in and of itself. (As are most ViewEngines, although it's not an obvious point to a lot of folks getting started with ASP.NET MVC or any MVC Framework)
请记住,这不是WebForms。 该<li>和each属性不是控件,也不是控件的一部分或runat = server或其他。 这是一个完整的ViewEngine。 它本身就是一种模板语言。 (与大多数ViewEngines一样,尽管很多人开始使用ASP.NET MVC或任何MVC框架并不是显而易见的一点)
That means that Louis has complete control of what the syntax is, and as you can see, it's somewhere in between HTML and Something Else. He's still changing the syntax based on feedback, so this is your chance to get involved.
这意味着Louis可以完全控制语法的含义,正如您所看到的,它位于HTML和Something Else之间。 他仍在根据反馈来更改语法,因此这是您参与的机会。
Here's Louis' Northwind "Listing By Category" page:
这是路易斯的罗斯文德“按类别列出”页面:
<viewdata
model="IList[[Product]]"
CategoryName="string"/>
${PageTitle(CategoryName)}
<CategoryMenuItems category="ViewData.Model.Select(p=>p.Category).FirstOrDefault()"/>
<ul class="productlist">
<var styles='new[] {"odd", "even"}'/>
<li each="var product in ViewData.Model" class="${styles[productIndex%2]}">
<ProductImage style='"float:left;"'/>
<p>
<a href="/Products/Detail/${product.ProductID}">${product.ProductName}</a>
<br />
Price: ${String.Format("{0:C2}", product.UnitPrice)}
<span class="editlink">
(${Html.ActionLink[[ProductsController]](c=>c.Edit(product.ProductID), "Edit")})
</span>
</p>
<div style="clear:both;"></div>
</li>
</ul>
![image](https://i-blog.csdnimg.cn/blog_migrate/e3cf830da9705495e7f9be194117f449.png)
Notice not only that the iterators live on the markup tags like <li> but also that you can create variables. See how an array called styles is created to hold the strings odd and even, then used to set the class on the <li> on the next line. The "jump into code block" is usually ${ } rather than <% %> which I find nice visually.
请注意,不仅迭代器存在于像<li>这样的标记标签上,而且还可以创建变量。 了解如何创建一个称为样式的数组来保存字符串的奇数和偶数,然后将其用于在下一行的<li>上设置类。 “跳入代码块”通常是$ {},而不是<%%>,我在视觉上觉得不错。
However, Spark is all about choice, so you can use EITHER ${ } or <% %>. Whatever makes you happy.
但是, Spark就是选择的全部,因此您可以使用$ {}或<%%>。 令您开心的事。
Also, check out the <CategoryMenuItems> tag. What's that? That's a partial view-file that gives you clean partial rendering.
另外,签出<CategoryMenuItems>标记。 那是什么? 这是一个局部视图文件,可为您提供清晰的局部渲染。
If you have a partial view file that are starts with an underscore, then you can call that from a regular view using its name as a tag. You can see two partial view files in the screenshot at right. It's really just a really pretty syntactic sugar over include files. Neat though!
如果您有一个以下划线开头的部分视图文件,则可以使用其名称作为标记从常规视图中调用该文件。 您可以在右侧的屏幕截图中看到两个局部视图文件。 它实际上只是包含文件上的一个非常漂亮的语法糖。 整洁!
Go check out Spark and hear more at Louis' Blog.
纳姆 (NHaml)
The deeply awesome Andrew Peters created NHaml (part of MVCContrib), an ASP.NET View Engine using the Haml syntax from Ruby. I've been showing Andrew's stuff as an extreme example of what a ViewEngine could look like.
令人敬畏的Andrew Peters使用Ruby的Haml语法创建了NHaml ( MVCContrib的一部分),这是一个ASP.NET View Engine。 我一直在向我展示Andrew的东西,作为ViewEngine外观的一个极端示例。
For example, here's a standard "Listing by Category" page using the built-in WebForms ViewEngine, written using NHaml. Notice the lack of angle brackets. Notice that the UL tag never ends...it just leaves scope. The whitespace is significant. Haml users feel strongly about not repeating themselves, even in markup. They want "Markup Haiku."
例如,这是一个使用内置的WebForms ViewEngine(使用NHaml编写)的标准“按类别列出”页面。 注意缺少尖括号。 请注意,UL标签永无止境...它只是离开了范围。 空格很重要。 Haml用户强烈希望不重复自己,即使在标记中也是如此。 他们想要“ Markup Haiku”。
#foo
- foreach (var product in ViewData)
- if (product.Category.CategoryName != null)
%h2=product.Category.CategoryName
- break
%ul.productlist
- foreach (var product in ViewData)
%li
= Html.Image("/Content/Images/" + product.ProductID + ".jpg", product.ProductName)
.productdetail
=Html.ActionLink(product.ProductName, "Detail", new { ID=product.ProductID })
%br
Price:
=String.Format("{0:C2}", product.UnitPrice)
%span.editlink
(
=Html.ActionLink("Edit", "Edit", new { ID=product.ProductID })
)
Other cool View Engines include Castle's Fork of NVelocity and Brail (in MVCContrib).
其他很棒的View引擎包括Castle的NVelocity叉和Brail (在MVCContrib中)。
Related Links
相关链接
知乎周源微信