对于.NET HTML生成?NET、HTML

2023-09-03 05:03:59 作者:神仙哥哥.

有使用海边过去的几年中,我发现模板系统是一个坏code气味。是否有使用类似于用于生成HTML,CSS和JavaScript的海滨帆布系统的东西。NET的框架?否则的方式,以避免重复我倾向于找到模板。

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.

NHaml不来接近我要找的。问题的关键是没有一个简写为(X)HTML,但有一种编程语言中,我可以重构和重新使用code。

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.

在海边,它可能是这样的:(画布是HTML [和javascript]的建造者)

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.

推荐答案

我有关于模板系统类似的感受(请参阅ASP.MVC: ?实现一个非模板视图引擎),并尝试了一点后,我采取了以下做法:

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:

实现不直接写入流的流畅HTML生成C#类,而是构建整个HTML页面在内存中的对象的层次结构。 在更高层次(可重复使用)件的HTML code(如复杂的输入控件等)实现为单独的类并插入作为节点到这个层次,而且可以自己自动扩展到普通的HTML节点。 MVC视图是那么POCO C#类在构成HTML层次结构并写入到响应流中。

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

Some of the benefits (from my perspective):

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