The Tag objects generate Html tags inside RazorBlade and provide a powerfull API will let you build html from code.
These docs will give you what you need to leverage the object.
Here you'll find:
- a Quick-Reference for the common API
- more instructions for doing specific things
- advanced API for special stuff
To see this in action with many examples, visit the RazorBlade Tutorials on 2sxc.org.
The following APIs will get you Tag objects:
Tag.*()where * is any standard HTML5 term, likeDiv,Hr,IFrameetc. - returns a typed object with more methods likeSrc(url)onTags.Img()v2.2Tag.*(content)same thing like above, but with content in the constructorTags.*(content1, content2, ...)same thing like above, but with much content in the constructorTag.Custom(string tagName)(more)
All these methods of a Tag change the object, and return the object itself again. This fluent-API allows chaining them together, like
someTag.Id("someId").Class("float-right")
Below we'll explain the methods that all Tag objects have, but many have custom commands as well.
For example, an Img Tag also has a Src(...) while anA Tag also has href.
Some of the additional commands are extra-smart and automatically URI encode the value if not yet encoded.
Attr(name, [value], [separator])
add an attribute - if it already exists, it will replace the value, unless a separator is specified which will then append the new value.name:string, requiredvalue:string|object|null, optional
Objectswill be JSON serialized;nullwill result in the attribute being added without a value, likedisabledseparator:string, default is ""
Separation character if we have to append to an existing value. If null, will replace instead of append.
Class(value)
set / add a class to the tag; if called multiple times, will append with a space between the original and new value. When calling with null, will reset the class to empty.Data(name, [value])
will add adata-{name}='value'attribute.Id(value)
set the id attribute - if called multiple times, will always replace previous idOn(name, jsCode)
add an event-attribute likeonclickStyle(value)
set / add a class to the tag; if called multiple times, will append with a semicolon;between the original and new value. When calling with null, will reset the id to empty.Title(value)
set the title attribute - if called multiple times, will always replace previous title
Add(value)
Add something to contents - at the end of existing content.value:string|Tag|IEnumerable<string|Tag>
Add(value, value, ...)v2.2
Add a many items to contents - at the end of existing content.value:string|Tag
Wrap(value)
Replaces the contentvalue:string|Tag|IEnumerable<string|Tag>
Wrap(value, value, ...)v2.2
Replaces the content with many itemsvalue:string|Tag
A Tag object and the two properties .Open and .Close all support IHtmlString, so you can output them directly:
@myTag
will render the tag into the html. Implements IHtmlString and will not be encoded.@myTag.TagStart
will render the opening tag to html. Implements IHtmlString and will not be encoded.myTag.TagEnd
will render the close-tag to html. Implements IHtmlString and will not be encoded.
_Note: the TagStart and TagEnd have unusually long names, to ensure that the terms won't collide with future attribute names like Open etc.
@using ToSic.Razor.Blade;
@Tags.Tag("br")
@Tags.Tag("div").Wrap("this is my message")
@{
var myStyle = Tags.Tag("style");
}
@myStyle.Open
.red { color: red};
@myStyle.CloseThe Tag object provides access to all known Html5 tags. Read the full API
