代码之家  ›  专栏  ›  技术社区  ›  Willis Blackburn

scala webapps中单独的HTML模板文件的基本原理?

  •  4
  • Willis Blackburn  · 技术社区  · 15 年前

    在斯卡拉中完成任何Web应用程序开发的每个人都知道可以使用任何现有的Java Web框架,或者可以使用升降机。Something that all of these frameworks have in common is that they all require two or more files to generate a page: an HTML template of some kind, and one or more classes to provide associated logic. Even if you're writing a JSP-only app, you're probably still making use of lots of custom tags.

    这让我想到了我注意到的一点:模板文件通常与HTML没有什么相似之处。Wicket模板文件几乎都是HTML,因为在Wicket中,组件将自己绑定到模板中的HTML标记。但是在所有基于自定义标记的框架中,模板通常都包含自定义标记,并且不能在浏览器中单独呈现。

    Scala supports embedding arbitrary XML directly into the program source. Here is a Scala function that make an unnumbered list from a collection:

    def listify[T](items: Iterable[T], liBody: T => NodeSeq) = <ul>{
      items.flatMap(i => <li>{liBody(i)}</li>)
    }</ul>
    

    Now if I have that, I've already abandoned architectural purity because I have ul and li tags in the "controller," or "business logic," or "backing object," or whatever you call it, and not in the template. Yet this is a pretty clear and straightforward way of creating a list. Doing it any other way requires substituting some run-time tag-binding framework in place of Scala's own built-in features.

    So I'm wondering, why not just go the other way and get rid of the templates? If Scala is useful for creating an unnumbered list, then why can't it also create whatever contains the unnumbered list? And all the way up the page back to the html tag.

    The question I'm asking is: given a language that already includes powerful support for XML, are there significant benefits to using template files and transforming them into actual HTML at run time, or is it better to just consider template files as artifacts carried over from languages without built-in XML support and abandon them?

    3 回复  |  直到 15 年前
        1
  •  2
  •   Jeremy Bell    15 年前

    I think the main reason is for tooling support. It is easier to support tooling when part of the project is strictly XML (the template files). It is far trickier (though not impossible), to manipulate code from a design tool.

    Also, in most production environments, the abstraction of business logic from the "view" (using whatever paradigm you like best, MVC, MVP, etc..) is critical. Any project large enough would likely be too unwieldy to go without it. Remember that in production environments, there are often more than one programmer, and sometimes new programmers join the team or existing ones move, so it's not really optional. Most of the efforts of web framework designers are invested in this use case, so functionality which breaks this abstraction is less of a priority.

    However, I could see where an internal scala DSL for web development might be useful for small projects. It would certainly not be unwise to invest your time in creating a light-weight framework for such use, if you would find it useful. Chances are, if you created it and found it useful, others might too.

    建议资源:

        2
  •  2
  •   user372388    15 年前

    简短的回答是:两者都是。

    You don't "have" to use templates even with Lift - you can write views directly in code. Little-documented, but possible.

    但是,如果您尝试,您将很快发现视图代码在布局和样式方面变得越来越难看。如果你想尝尝,可以尝试复制,比如说,原始scala;中的facebook首页。

    If more than 2-3 people are working on a project, separation of responsibilities naturally arises. 模型视图控制器之所以流行,并不是因为Web是它的一个很好的例子(它是它的一个真正的例子),但是因为它是这样一种职责分离的最佳逼近——HTML/CSS/JavaScript通常不是PHP/Java/Scala人,并且这两种类型都需要能够独立地生产。

    In short, if you wish to write your views in Scala, may I advise figuring out how to do it in Lift - you get generous amounts of other tasty booty for taking the time to learn it. Once you've written perhaps an app or two that way, ask yourself if it was an experience you 'd wish to repeat. :)

        3
  •  1
  •   Community CDub    8 年前

    Your argument with the powerful XML support works both ways, I think. Because of the powerful XML support, it is possible to define your own tags in the template not only to bind variables but also to do some XML transformations inside your template.

    For example, you can define a wrapper tag which takes all inner table nodes and adds odd and even classes to each of the rows. (见 this 当然,您也可以在listify(或tabulify)方法中进行此操作,但这样会失去更多的纯度。

    For simple applications with just a few controllers, I think it is possible to do without templates as is shown by the already quoted step framework . You just say ‘print that list’ and are done. For larger frameworks however, your logic obviously gets more and more complicated. There needs to be a way to tell the application when and where each controller needs to be called. You’re likely to define that in a single XML document – and there it is again, your XML template you wanted to get rid of.
    And then, depending on who is going to design the template, you either want to allow code in XML or you want to avoid it. And because of scala’s XML processing ease, you won’t actually miss it that much when you avoid it, for you have other ways to work around it.

    Now, the lift XML templates are decidedly non-scala-esque, so there is no direct way to insert scala-logic. Other frameworks might have chosen a different route. But also in lift, I think, you could just create a custom XML tag for your HTML body and then have a function where you deliberately mix scala code with XML stuff to produce your output.

    推荐文章