.Net 模板引擎,具有对象图输出,而不是字符串

发布于 2024-10-05 01:05:54 字数 1379 浏览 4 评论 0原文

我想知道是否有用于对象图创建的模板引擎?有许多方法可以创建 HTML 或其他字符串结果内容。但我想要一个能够吐出对象的模板引擎。可以说是预处理器。

我有一个应用程序需要某种预定义的对象图,用于呈现特定的页面小部件。例如,我的页面上有一个工具栏,但每个页面都有不同的工具栏。它可能看起来非常相似。但例如按钮“添加”将指向一个页面上与另一页面上不同的 URL。

示例对象图(部分):

IList<ToolbarItem> toolbar = new List<ToolbarItem> {
    new ToolbarButton {
        Text = PageResources.NewText,
        Url = Url.Action("Add", "Items"),
        Icon = ToolbarIconType.New
    },
    new ToolbarSeparator(),
    new ToolbarDropDown {
        Text = PageResources.DropDownText,
        Icon = ToolbarIconType.Filter,
        Items = new List<ToolbarDropDownItem> {
            new ToolbarDropDownItem {
                Text = PageResources.FirstItem,
                Url = Url.Action("Whatever", "Filter")
            },
            new ToolbarDropDownItem {
                Text = PageResources.SecondItem,
                Url = Url.Action("BlahBlah", "Filter"),
                Icon = ToolbarIconType.Search
            },
            ...
        }
    },
    ...
};

这个静态定义(带有动态资源和动态 URL 帮助程序调用)可能会变得相当长和复杂。想象一下类似于 Word 的带状工具栏。非常复杂而且很长。

我希望将这些对象生成模板存储在数据库中,并且每个页面都能够读取自己的模板,然后模板引擎将生成它并使用特定的资源类型+密钥来放入本地化数据。操作也是如此' URL...

类似的事情在对象反序列化(XML 或 JSON)中完成。但内置(反)序列化技术不支持变量替换或资源链接或类似技术。最好的事情是支持资源链接和 lambda 表达式。

总结一下:是否有任何 .net 对象图模板引擎/预处理器可以提供我所需要的内容?

I would like to know if there's any templating engine that is used for object graph creation? There are many to create HTML or other string resulting content. But I would like a templating engine that spits out objects. So to speak a preprocessor.

I have an application that needs some sort of predefined object graphs that's used to render a specific page widget. For instance I have a toolbar on my pages but every page has a different toolbar. It may look very similar. but for instance button Add will point to a different URL on one page than on the other.

Example object graph (part of it):

IList<ToolbarItem> toolbar = new List<ToolbarItem> {
    new ToolbarButton {
        Text = PageResources.NewText,
        Url = Url.Action("Add", "Items"),
        Icon = ToolbarIconType.New
    },
    new ToolbarSeparator(),
    new ToolbarDropDown {
        Text = PageResources.DropDownText,
        Icon = ToolbarIconType.Filter,
        Items = new List<ToolbarDropDownItem> {
            new ToolbarDropDownItem {
                Text = PageResources.FirstItem,
                Url = Url.Action("Whatever", "Filter")
            },
            new ToolbarDropDownItem {
                Text = PageResources.SecondItem,
                Url = Url.Action("BlahBlah", "Filter"),
                Icon = ToolbarIconType.Search
            },
            ...
        }
    },
    ...
};

This static definition (with dynamic resources and dynamic URL helper calls) may become quite long and complex. Think of a ribbon-like toolbar similar to Word. Very complex and long.

I would like to have these object generation templates stored in the database and each page would be able to read their own, templating engine would then generate it and use particular resource type + key to put localised data in. The same thing would be with actions' URLs...

Something similar is done at object deserialization (XML or JSON). but built-in (de)serialization technologies don't support variable replacements or resource links or similar. Best thing would be to support resource links and lambda expressions.

To sum it up: is there any .net object graph templating engine/preprocessor that would provide what I need?

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

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

发布评论

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

评论(2

糖果控 2024-10-12 01:05:54

这是一个非常有趣的问题。

我认为一种选择是生成 XAML,然后使用 XamlReader.Load (参见 MSDN) 加载 XAML 描述的对象树(或图)。请注意,XAML 可用于构造任何遵循某些基本要求的 .NET 对象(例如,具有公共 get/set 属性并实现某些接口(如果它们应可用作集合)。

此外,XAML 是一个 XML 文档,因此应该可以使用可以生成 XML 文件的标准文本模板引擎来生成 XAML 文档。

That's a very interesting question.

I think that one option would be to generate XAML and then use XamlReader.Load (see MSDN) to load the object tree (or graph) described by the XAML. Note that XAML can be used to construct any .NET objects that follow some basic requirements (e.g. have public get/set properties and implement some interface if they should be useable as collections).

Also, XAML is a XML document, so it should be possible to use stnadard text templating engines that can produce XML files to generate your XAML documents.

剩一世无双 2024-10-12 01:05:54

JSON 序列化

我决定使用 Json.Net 库主要是因为编写 JSON 字符串非常容易被人类阅读,或者我应该说Web 开发人员可读并且易于维护。它与普通的 Javascript JSON 非常相似,但具有额外的语法糖(引用保存、类型保存等)。Json.NET

库是提供的 JavaScriptSerializerDataContractSerializer 的一个非常好的升级。类。

JSON serialization

I decided to use Json.Net library mostly because writing JSON strings is very human readable or should I rather say web developer readable and is easy to maintain. It's very similar to normal Javascript JSON but with additional syntactic sugars (referential preservation, type preservation etc.)

Json.NET library is a very good upgrade from provided JavaScriptSerializer or the DataContractSerializer classes.

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