比较 Haskell 的 Snap 和 Yesod Web 框架
最近新闻中的两个 Haskell Web 框架是 Yesod (0.8)和 Snap(0.4)。
很明显,Yesod 目前支持的功能比 Snap 多得多。然而,我无法忍受 Yesod 使用的 HTML、CSS 和 Javascript 语法。
所以,我想了解如果我选择 Snap,我会错过什么。例如,看起来不存在数据库支持。会议怎么样?其他功能?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
全面披露:我是 Snap 的主要开发人员之一。
首先我们先来说一下什么是Snap。目前,Snap 团队在 hackage 上维护着五个不同的项目:snap-core、snap-server、heist、snap 和 xmlhtml。 snap-server 是一个 Web 服务器,公开由 snap-core 定义的 API。抢劫是一个模板系统。 xmlhtml 是 heist 使用的 XML/HTML 解析和渲染库。 snap 是一个伞式项目,它将它们粘合在一起,并提供强大的 snaplets API,使 Web 应用程序可组合和模块化。
Yesod 有许多关于 hackage 的项目。其中大多数(全部?)都列在Yesod 类别中。一些著名的有 yesod-core、warp、perspective 和 hamlet。
Haskell Web 开发的现实是,它远不像人们想象的那样是一种排他或选择。一般来说,这些项目是非常松散耦合的并且相当可互换。您可以使用 warp(Yesod 团队的 Web 服务器)、heist(Snap 团队的模板系统)和 Acid-state(Happstack 项目的持久性系统)构建网站。您还可以将 snap-server 与 hamlet 或 permanent 结合使用。
也就是说,这两个项目肯定存在一些差异。我可以客观地指出的最大区别是,Yesod 项目通常大量使用 Template Haskell 和准引用来创建简洁的 DSL,而 Snap 项目则坚持构建有利于可组合性的组合器库。我能想到的任何其他差异都会在主观上偏向 Snap。以两个项目命名的伞包显然将为上述组件做出特定的选择,这些选择将反映在项目依赖项中。但这仍然并不意味着您不能引入不同的东西并使用它。
Snap 确实有 会话 和 身份验证、与多个数据库的接口以及良好的表单处理(此处和此处)使用digestive-functors,其中包括对任意嵌套的预打包支持动态调整大小的列表。这些只是不断发展的可插式 snaplet 生态系统的一部分。会话和身份验证 snaplet 以与后端无关的方式编写。因此,通过少量的粘合代码,您应该能够将它与您能想到的任何持久性系统一起使用。未来,Snap 将尽可能坚持这一政策。
在大多数情况下,我认为 Snap、Yesod 和 Happstack 的选择不是功能问题,而是个人品味问题。每当有人说其中一个框架没有另一个框架所具有的功能时,大多数时候,通过导入必要的包,可以很容易地从另一个框架中获取缺少的功能。
编辑:有关三大 Haskell Web 框架的更详细比较,请查看我最近的 Haskell Web Framework比较矩阵
Full disclosure: I'm one of the lead developers of Snap.
First of all, let's talk about what Snap is. Right now the Snap team maintains five different projects on hackage: snap-core, snap-server, heist, snap, and xmlhtml. snap-server is a web server that exposes the API defined by snap-core. heist is a templating system. xmlhtml is an XML/HTML parsing and rendering library used by heist. snap is an umbrella project that glues them all together and provides the powerful snaplets API that makes web apps composable and modular.
Yesod has a host of projects on hackage. Most (all?) of them are listed in the Yesod category. Some of the notable ones are yesod-core, warp, persistent, and hamlet.
The reality of Haskell web development is that it's much less of an exclusive-or choice than seems to be perceived. In general the projects are very loosely coupled and fairly interchangeable. You could build a website using warp (the Yesod team's web server), heist (the Snap team's template system), and acid-state (the Happstack project's persistence system). You could also use snap-server with hamlet or persistent.
That said, the two projects definitely have some differences. The biggest difference I can point out objectively is that Yesod projects typically make heavy use of Template Haskell and quasiquoting to create concise DSLs, while Snap projects stick to building combinator libraries that favor composability. Just about any other differences I can think of will be subjectively biased towards Snap. The umbrella packages named after both projects are obviously going to make specific choices for the above mentioned components, and these choices will be reflected in the project dependencies. But that still doesn't mean that you can't pull in something different and use it as well.
Snap does have sessions and authentication, interfaces to several databases, and nice form handling (here and here) using digestive-functors that includes prepackaged support for arbitrarily nested dynamically sizable lists. These are just some of the growing ecosystem of pluggable snaplets. The sessions and authentication snaplets are written in a way that is back-end agnostic. So with a small amount of glue code you should be able to use it with just about any persistence system you can think of. In the future, Snap will stick with this policy as often as possible.
For the most part I think the choice of Snap vs Yesod vs Happstack is less an issue of features and more one of personal taste. Whenever someone says that one of the frameworks doesn't have something that another one has, most of the time it will be pretty easy to pull in the missing functionality from the other framework by importing the necessary package.
EDIT: For a more detailed comparison of the big three Haskell web frameworks check out my recent blog post. For a rougher (but possibly more useful) comparison using some broader generalizations, see my Haskell Web Framework Comparison Matrix
公平警告:我是 Yesod 的首席开发人员。
我不确定你不喜欢 JavaScript 语法的哪一点:它是带有变量插值的纯 JavaScript。至于 CSS,Yesod 现在有 Lucius,它允许您也使用纯 CSS。对于 HTML,您可以轻松使用任何其他您想要的库,包括 Heist(Snap 使用的库)。也就是说,跳过 Yesod 而不是 CSS/Javascript 语法是一件很有趣的事情,因为 Snap 甚至没有相应的语法。当然欢迎您使用他们的静态文件解决方案。
Yesod 提供对身份验证/授权、类型安全 URL、小部件、电子邮件以及各种小东西(面包屑、消息、最终目的地)的无缝支持。另外,Yesod 有一套相当丰富的附加包,用于注释和降价等,还有一些大型的实际代码库可供选择作为示例。如果其中任何一个对您有吸引力,您可能需要检查您的替代方案是否支持它们。
Fair warning: I'm the lead developer of Yesod.
I'm not sure what you don't like about the Javascript syntax: it is plain javascript with variable interpolation. As for CSS Yesod now has Lucius which allows you to also use plain CSS. For HTML, you can easily use any other library you want, including Heist (what Snap uses). That said, it's a bit of a funny thing to skip Yesod over CSS/Javascript syntax, when Snap doesn't even have a syntax for it. You are certainly welcome to their solution of just static files.
Yesod comes with seamless support for authentication/authorization, type-safe URLs, widgets, email, and a bunch of little things all over the place (breadcrumbs, messages, ultimate destination). Plus, Yesod has a fairly rich set of add-on packages for things like comments and markdown, and a few large real-world code bases to pick at for examples. If any of these are attractive to you, you might want to check if your alternatives supports them.
尝试一下《哈姆雷特》——您可能最终会喜欢它。表面上的负面反应并不少见。然而,真正使用过哈姆雷特的人并没有抱怨。
另外,为什么不使用 Happstack 呢?仅仅因为它们没有“出现在新闻中”并不意味着它们没有坚实的框架。
Give hamlet a try- you might end up liking it. A negative reaction at a superficial level is not uncommon. However, nobody that has actually used hamlet complains.
Also, why not use Happstack? Just because they aren't "in the news" doesn't mean they don't have a solid framework.
您可能指的是旧版本的 yesod。最新的 yesod 版本具有 html、javascript 和 css 的简单语法。
yesod 模板库 hamlet 的 html 语法是纯 html,具有完整的开始和结束标记以及所有正常的 html 属性。是的,您可以省略结束标签并使用 id 和 class 属性的快捷方式。但你不必这样做。您可以继续编写纯 html。
不仅如此,html 模板还可以驻留在单独的文件中,就像 Snap 的模板库 Heist 中一样。
Java 脚本模板 (julius) 是纯 JavaScript 文件,也驻留在单独的文件中。
css 模板确实有不同的语法,但最新版本的 yesod 现在也提供简单的 css 语法。
如果您使用 Heist,您将不会获得类型安全的 url。
在 Heist 中,每次都会从硬盘读取 html 模板。 Yesod 将所有模板直接编译为可执行文件。没有从硬盘读取文件。因此响应速度要快得多。您可以自己查看基准。
在 Yesod 中,您可以创建配合良好的小部件。 Snap 根本不处理小部件。你必须自己动手。
You probably referring to old version of yesod. Latest yesod versions have plain syntax for html, javascript and css.
The html syntax of yesod's template library hamlet is plain html with complete opening and closing tags and all normal html attributes. Yes you can omit closing tags and use shortcuts for id and class attributes. But you do not have to. You can continue to write plain html.
Not only that but html templates can reside in separate files, just like in Snap's template library Heist.
Java script templates (julius) are plain javascript files, also residing in separate files.
The css template does indeed have a different syntax, but recent version of yesod now provides also plain css syntax.
If you go with Heist you will not have type safe urls.
In Heist html templates are read from harddrive everytime. Yesod compiles all templates directly into the executable. No file is read from harddrive. Thus the response is much faster. You can see the benchmarks yourself.
In Yesod you can create widgets that cooperate nicely. Snap does not deal with widgets at all. You will have to roll your own.