在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:HtmlTags/htmltags开源软件地址:https://github.com/HtmlTags/htmltags开源编程语言:C# 96.6%开源软件介绍:HtmlTags.NET objects for generating HTML InstallationInstall via nuget, for ASP.NET Core:
When should I use HtmlTags?In general, you should avoid building strings of HTML in your applications. There are plenty of template/view engines that are much more suitable for generating dynamic markup. However, there are some situations that require you to build snippets of HTML from code (e.g., view extensions in HtmlHelper extensions in ASP.NET Core). HtmlTags is the best way to build those snippets. The HtmlTags advantage
That last point seems trivial, but it is actually HtmlTag's biggest strength. If your HtmlHelper extensions return an HtmlTag instance, you have the ability to customize the generated tag within your view template. When your HtmlHelpers return a strings (as the helpers included with ASP.NET Core do), you have no chance to customize the tag (without ugly string manipulation). HtmlHelpers that return stringsConsider the built-in ASP.NET Core TextBox() helper:
It will generate the following HTML:
What if you wanted to apply a CSS class to that input? Or change the element id? You can do it, but it requires a much more verbose overload:
HtmlHelpers that return HtmlTagsNow consider an HtmlHelper extension that returns an HtmlTag:
You can use it just as easily in your view template:
and it will generate the exact same HTML as above. However, you can also very easily modify the tag produced by the helper:
will generate:
UsageOverviewEach HtmlTag instance represents a single HTML element, and optionally, a collection of inner elements. To create an element, pass the element name to the HtmlTag constructor:
Calling ToString() on this instance will produce:
There are a number of methods you can use to modify the element:
which will produce:
All of the modifying methods return the instance, so they can be chained. The above example can be rewritten as:
Most methods that set a value also have an overload that returns the value:
Custom Data attributesThere is also built-in support for JSON serializing values of HTML5-style custom data attributes:
Will render:
jQuery Metadata supportOr you can serialize an entire settings object in a single value on the server that can be interpreted on the client using something like the jQuery Metadata plugin:
Notice we're passing an instance of an object (EditableOptions) as the value of the "editable" metadata. This will render:
You can then drive logic on the client using this data:
More informationMore detailed documention is coming. In the meantime, try browsing the unit tests for further examples. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论