.NET 的 HTML 生成器?

发布于 2024-07-21 05:14:50 字数 972 浏览 5 评论 0 原文

在过去几年使用 Seaside 后,我发现模板系统的代码味道很糟糕。 是否有一个 .net 框架使用类似于 Seaside 画布系统的东西来生成 html、css 和 javascript? 或者是一种避免我在模板中发现重复的方法。

[编辑] NHaml 与我正在寻找的东西并不接近。 重点不是拥有 (X)HTML 的简写,而是拥有一种可以重构和重用代码的编程语言。

在 Seaside 中,它可能看起来像这样:(画布是 html [和 javascript] 的构建器)

renderContentOn: canvas
    canvas form
        class: 'eventEditor';
        with:[
            self renderWhoOn: canvas;
                 renderButtonsOn: canvas]

在这个方法中,我调用两个子例程

renderWhoOn: canvas
self decorateDivAndLabel: 'Who' on: canvas around: [
    canvas select
        id: tagId;
        selected: model who;
        list: model whoList;
        callback: [:value | model who: value]]

第一个调用围绕选择表单元素的装饰器:

decorateDivAndLabel: aString on: canvas around: aBlock
canvas div: [
    canvas label
        for: (tagId := canvas nextId);
        with: aString,':'.
    aBlock value]

这允许消除几乎所有重复。

Having used Seaside the past few years I've found template systems to be a bad code smell. Is there a framework for .net using something similar to the Seaside canvas system for generating html, css and javascript? Or else a way to avoid the duplication I tend to find in templates.

[Edit]
NHaml does not come close to what I'm looking for. The point is not having a shorthand for (X)HTML, but having a programming language in which I can refactor and reuse the code.

In Seaside, it might look like this: (the canvas is the builder of html [and javascript])

renderContentOn: canvas
    canvas form
        class: 'eventEditor';
        with:[
            self renderWhoOn: canvas;
                 renderButtonsOn: canvas]

In this method, I call two subroutines

renderWhoOn: canvas
self decorateDivAndLabel: 'Who' on: canvas around: [
    canvas select
        id: tagId;
        selected: model who;
        list: model whoList;
        callback: [:value | model who: value]]

The first one calls a decorator around a select form element:

decorateDivAndLabel: aString on: canvas around: aBlock
canvas div: [
    canvas label
        for: (tagId := canvas nextId);
        with: aString,':'.
    aBlock value]

This allows eliminating almost all duplication.

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

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

发布评论

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

评论(2

何以笙箫默 2024-07-28 05:14:51

我对模板系统有类似的感觉(请参阅 ASP.MVC:实现非-模板化视图引擎?),经过一些尝试,我采用了以下方法:

  • 实现了一个流畅的 HTML 生成器 C# 类,它不直接写入流,而是将整个 HTML 页面构造为一个 in-内存对象层次结构。
  • 更高级别(可重用)的 HTML 代码片段(如复杂的输入控件等)被实现为单独的类,并作为节点插入到此层次结构中,并且可以自动扩展为纯 HTML 节点。
  • MVC 视图是 POCO C# 类,它构造 HTML 层次结构并将其写入响应流。

一些好处(从我的角度来看):

  • 您仍然保留对 HTML 的控制
  • 可重用性、继承、封装...
  • 结果是自动格式化的(缩进、XHTML 等)
  • 并且最重要的是...没有使用单独的模板脚本DSL。

I have similar feelings about template systems (see ASP.MVC: Implementing a non-templated view engine?), and after experimenting a little, I took the following approach:

  • Implemented a fluent HTML generator C# class which does not write directly to a stream, instead it constructs the whole HTML page as an in-memory object hierarchy.
  • Higher-level (reusable) pieces of HTML code (like complex input controls etc.) are implemented as separate classes and are inserted as nodes into this hierarchy, and can expand themselves into plain HTML nodes automatically.
  • MVC Views are then POCO C# classes which construct the HTML hierarchy and write it out into the response stream.

Some of the benefits (from my perspective):

  • You still retain control over HTML
  • Reusability, inheritance, encapsulation...
  • The result is auto-formatted (indents, XHTML etc.)
  • And best of all... no template scripting using a separate DSL.
壹場煙雨 2024-07-28 05:14:51

我不熟悉 Seaside ,但您可以使用 许多不同的视图引擎与 ASP.NET MVC,例如,NHaml。 也许您可以评估这些并看看其中是否适合您的需求。

您还可以创建您自己的视图引擎

I am not familiar with Seaside , but you can use many different view engines with ASP.NET MVC, e.g., NHaml. Perhaps you can evaluate those and see if one would fit your needs.

You can also create your own view engine.

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