使用替代视图引擎有哪些好处?

发布于 2024-08-10 11:00:45 字数 806 浏览 5 评论 0原文

我正在使用 ASP.NET MVC,发现有一些可用的替代视图引擎,例如 NHaml 和 Spark。我的问题是为什么您要使用备用视图引擎?我不认为拥有这样的东西有什么好处:

<ul if="products.Any()">
    <li each="var p in products">${p.Name}</li>
</ul>
<else>
    <p>No products available</p>
</else>

使用 Spark 视图引擎(自从那以后,我还没有使用 Spark 来验证这一点,并且可能完全错误,因为你通过了,所以你不会得到 Intellisense代码作为字符串)以及:

<% if products.Any() { %>
    <ul>
      <% foreach (var p in products) { %>
        <li><%= p.Name %></li>
      <% } %>  
    </ul>
<% } else { %>
    <p>No products available</p>
<% } %>

使用内置的 ASP.NET MVC 模板格式(尽管我承认悬挂的大括号非常难看)。除了不喜欢“gator”标签(或悬挂的大括号)之外,是否有任何合理的理由考虑使用备用视图引擎?或者只是因为它是新事物而很酷?

I am playing with ASP.NET MVC and I see that there are a few alternate view engines available for it such as NHaml and Spark. My question is why would you use an alternate view engine? I don't see a benefit to having something like this:

<ul if="products.Any()">
    <li each="var p in products">${p.Name}</li>
</ul>
<else>
    <p>No products available</p>
</else>

using the Spark view engine (doubly so since, and I haven't used Spark to verify this and might be totally wrong, you wouldn't get Intellisense since you're passing the code as a string) and:

<% if products.Any() { %>
    <ul>
      <% foreach (var p in products) { %>
        <li><%= p.Name %></li>
      <% } %>  
    </ul>
<% } else { %>
    <p>No products available</p>
<% } %>

using the built-in ASP.NET MVC template format (although I admit the dangling curly brace is pretty ugly). Is there any legitimate reason apart from not like the "gator" tags (or dangling curly braces) to consider using an alternate view engine? Or is it just cool because it's something new?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半﹌身腐败 2024-08-17 11:00:45

尝试让它变得更复杂一点:

<div if="orders.Any()" each="var order in orders">
  Here's your order #${orderIndex+1}:
  <ul>
    <li each="var p in order.Products">
       ${pIndex}: ${p.Name}
       <span if="pIsLast"> (the end)</span>
    </li>
  </ul>
</div>

我可以在这里看到流程。我实际上可以在这里看到HTML。现在看看:

<% if (orders.Any()) { %>
  <% var orderIndex = 0; foreach (var order in orders") { %>
   <div>
    Here's your order #<%= (orderIndex+1) %>
    <ul>
      <% int pIndex = 0; foreach (var p in order.Products) 
         { bool pIsLast = pIndex == products.Count; %>
        <li>
           <%= pIndex %>: <%= p.Name %>
           <% if (pIsLast) { %>
              <span> (the end)</span>
           <% } %>
        </li>
      <% ++ pIndex; } %>  
    </ul>
   </div>
  <% orderIndex++; } %>
<% } %>

我在这里迷路了。那里有 HTML 吗?

对我来说,这是主要原因。当然,Spark 提供了大量的功能 - 宏(在 Spark 标记中编码 Html.Helpers)、PDF 导出等 - 由其他人列出 - 但作为一名程序员,我更喜欢干净的代码。

再举个例子,你经常使用 for (int i = 0; i < products.Count; i++) { Product Product = products[i]; 吗? .... } 这些日子?您更喜欢 foreach (var products in products) {} 吗?我会的。不仅是因为打字更少。因为:

  • 它更好地表达了意图,
  • 读起来更好,
  • 它隐藏了额外的细节(例如 .Count、.Length 或 .Count();或者如果它是 IEnumerable,你必须以特殊的方式遍历),
  • 它减少了我疲惫的头脑中的数量。变量使上下文混乱(以及我疲倦的头脑),
  • 因此我可以专注于问题,没有任何事情
  • 可以帮助避免 {},因为不需要在内部有一个变量 - 减少行数
  • 以及当您从 IList 更改集合时到数组,你不需要到处改变 50 个循环

,这就是像 foreach 这样简单的事情。每个原因都很简单,但它们概括起来却有更大的意义。这些原因完全适用于 Spark。嘿,看起来我恋爱了;-)

更新:现在,你知道吗?查看我的帖子的编辑历史。我不得不多次编辑该死的 ASP 代码,只是因为我遗漏了一些地方。我只是不知道这是对还是错。如果我在那里有跨度,或者有条件的话,它是完全隐藏的。

更新:嗯,又一个编辑...将 ASP 的 if/span 移到 li 中...

Try to make it a bit more complex:

<div if="orders.Any()" each="var order in orders">
  Here's your order #${orderIndex+1}:
  <ul>
    <li each="var p in order.Products">
       ${pIndex}: ${p.Name}
       <span if="pIsLast"> (the end)</span>
    </li>
  </ul>
</div>

I can see the flow here. I can actually see HTML here. Now take a look:

<% if (orders.Any()) { %>
  <% var orderIndex = 0; foreach (var order in orders") { %>
   <div>
    Here's your order #<%= (orderIndex+1) %>
    <ul>
      <% int pIndex = 0; foreach (var p in order.Products) 
         { bool pIsLast = pIndex == products.Count; %>
        <li>
           <%= pIndex %>: <%= p.Name %>
           <% if (pIsLast) { %>
              <span> (the end)</span>
           <% } %>
        </li>
      <% ++ pIndex; } %>  
    </ul>
   </div>
  <% orderIndex++; } %>
<% } %>

I'm lost here. Is there any HTML there?

For me, this is the main reason. Of course, Spark gives TONS of features - macros (where you code your Html.Helpers in spark markup), PDF export, etc - listed by others - but as a programmer I prefer clean code.

As another example, do you often use for (int i = 0; i < products.Count; i++) { Product product = products[i]; .... } these days? Would you prefer foreach (var product in products) {}? I would. Not only because it's less to type. Because:

  • it expresses the intent better
  • it reads better
  • it hides additional details (like .Count, .Length, or .Count(); or if it's IEnumerable that you have to traverse in a special way) from my tired mind
  • it reduces number of variables cluttering the context (and my tired mind)
  • thus I can concentrate on the problem, nothing goes in the way
  • it helps to avoid {} since there's no need to have a variable inside - reducing line count
  • and when you change the collection from IList to array, you don't change 50 loops all over the place

and that's about such simple thing as foreach. Each reason is simple, but they sum up to something bigger. And these reasons apply perfectly to Spark. Hey, looks like I'm in love ;-)

Update: now, you know what? Look at my post's edit history. I had to edit the damn ASP code several times just because I missed some bits here and there. I just do not see if it's right or wrong. It's completely hidden if I have the span there, or condition at place.

Update: hm, yet another edit... moving ASP's if/span inside li...

蘑菇王子 2024-08-17 11:00:45

我认为您需要问的问题是“为什么要更改视图引擎”而不是“我应该更改视图引擎”

我选择 Spark 是因为我想要更清晰的视图,而且我还想用它来为除HTML。我目前使用它来生成 XML、JSON 和电子邮件模板,因此它已成为我的模板引擎和视图引擎。这是可能的,因为它允许您将视图渲染为字符串,而这在标准视图引擎中(不容易)可用。

同样重要的是要注意,您也不必使用单个视图引擎。您可以同时使用多个引擎,因此您可以使用 HTML 模板的默认视图引擎,但切换到 XML 的 Spark。

总的来说,如果默认视图引擎正在做您需要的一切并且您对此感到满意,那么我根本不会费心切换,但是如果您有默认视图引擎无法满足的特定需求,那么也许是时候了寻找替代方案。

I think the question you need to ask is "why you want to change view engines" rather than "Should I change view engines"

I chose spark because I wanted cleaner looking views and I also wanted to use it to create templates for things other than HTML. I currently use it for generating XML, JSON and email templates so it's become a templating engine for me as well as a view engine. This is made possible because it allows you to render views to strings rather, which isn't (easily) available in the standard view engine.

It's also important to note that you don't have to use a single view engine either. You can use multiple engines at the same time so you might use the default view engine for HTML templates, but switch to spark for XML.

Overall, if the default view engine is doing everything you need and you're happy with it then I wouldn't bother switching at all, however if you have specific needs that aren't being met by the default view engine then maybe it's time to look at alternatives.

寄与心 2024-08-17 11:00:45

就我个人而言,我不使用替代视图引擎,但它们的吸引力在于更清晰的视图。如果您精通 Spark,第一个示例会更好、更容易阅读。

但是,我更喜欢将诸如您所提供的逻辑包装到辅助方法中。我不需要学习新的东西,我的同事都能理解,我的观点也比较干净。这是一个完全主观的问题,任何一种方法都可以。

这些视图引擎很大程度上是从 RoR 和 Django 等其他框架继承而来的。 Django 对其视图模板系统的争论是视图应该用于视图逻辑。因此,如果您限制视图引擎中的功能,那么您混合控制器和视图之间职责的机会就会减少。

Personally, I don't use alternate view engines, but their appeal is that of cleaner views. If you're fluent in Spark, that first example is a lot nicer and easier to read.

However, I prefer to wrap logic such as what you have given into a helper method. I don't have to learn something new, all of my coworkers can understand it, and my views stay relatively clean. It's a completely subjective matter, and either approach is fine.

These view engines are largely a carry-over from other frameworks like RoR and Django. Django's argument for its view templating system is that views should only be for view logic. Therefore, if you restrict what is possible in your view engine, you have fewer opportunities to mix up responsibilities between controllers and views.

番薯 2024-08-17 11:00:45

我想说这比其他任何事情都更像是一种选择。这就像在 C# 和 Visual Basic 之间做出选择一样,使用您最了解的并且可以提高工作效率的内容。

I'd say it's more of an option than anything else. It's like deciding between C# and Visual Basic, use what you know best and what you will be more productive in.

黎歌 2024-08-17 11:00:45

Spark 的一些好处(我喜欢的):

  1. 您可以使用部分视图,例如 html 标签。使用示例可以是圆角标记。
  2. 你不会得到“标签汤”。也许你现在看不到好处,但如果你有很大的观点,它就更具可读性。
  3. 您可以获得对 ViewData["..."] 的强类型访问。
  4. 您可以轻松地将视图渲染为字符串。
  5. 自动应用 Html.Encode,提高应用程序的安全性。

我不喜欢的地方:

  1. Intellisense 和 Resharper 存在问题。
  2. 无法使用“格式化文档”选项。

我 Spark 已经解决了这个问题,我认为没有理由使用标准视图引擎。

Some of benefits of Spark (what I like):

  1. You can use partial views like html tags. Example of use can be tag for rounded corners.
  2. You don't get "tag soup". Maybe you don't see benefit right now, but if you have huge views, it is much more readable.
  3. You get strongly typed access to ViewData["..."].
  4. You can easily render views to string.
  5. Automatically applies Html.Encode, increasing safety of your application.

What I don't like:

  1. Has issues with Intellisense and Resharper.
  2. Can't use Format document option.

I Spark had this issues solved, I would see no reason to use standard view engine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文