知乎周源微信_每周源代码30-Spark和NHaml-疯狂的ASP.NET MVC ViewEngines

本文介绍了两种独特的ASP.NET MVC视图引擎——Spark和NHaml。Spark提供了一种让HTML主导流程并允许代码无缝融入的方式,拥有独特的语法特性,如在标记上使用`each`属性。而NHaml则采用Ruby的Haml语法,以简洁的格式编写视图。这些视图引擎为开发者提供了更多选择和更高效的开发体验。
摘要由CSDN通过智能技术生成
知乎周源微信

知乎周源微信

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中的可扩展性点,但是却不如某些人那样。 SparkLouis 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

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使用RubyHaml语法创建了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

相关链接

翻译自: https://www.hanselman.com/blog/the-weekly-source-code-30-spark-and-nhaml-crazy-aspnet-mvc-viewengines

知乎周源微信

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值