App
Requirements
RazorBlade Basic Html5 Tag
API Introduction v3.0
Learn how to use ToSic.Razor.Blade
fluent Tag
API to do really cool stuff. This uses ToSic.Razor.Blade.
This API lets you safely create html from code in an elegant way. This basic introduction shows the most basic principles you can apply, and we'll build on that in the next tutorials.
Basic Principles
- Quick, Safe Construction
@Tag.Div()
will created typed objects to generate Html5. TheDiv
can be any known Html5 tag, so@Tag.Img()
,@Tag.Table()
,@Tag.Br()
will all work.
👉 Here is the complete list - Wrap Text and Tags in the constructor
Use@Tag.Div("hello!")
to put content into a tag. You can also put tags inside tags like@Tag.Div(Tag.H3("a title"))
, and also add an unlimited list of things with@Tag.Div(Tag.H3("Title"), Tag.P("some intro text"), ...)
- Add more Text & Tags later on
Sometimes your code will want to add content step-by-step. You can always do this using.Add(...)
, so you can write@Tag.Div(Tag.H3("title")).Add(Tag.P("body"))
- Replace the contents completely
Maybe you've contstructed some tags but now the code needs to throw away the previous content and replace it. Use.Wrap(...)
for this. So@Tag.Div(Tag.H3("...")).Wrap("just reset the content")
will completely replace the whole contents with the text. - Set Common Attributes
Each tag object has many commands on it likeId(...)
,Class(...)
,Style(...)
,Title(...)
. The result of these commands is always the original object, so you can chain these (fluid API). So you can write@Tag.Div().Id("myId").Class("row highlighted")
.
Important: Some attributes are extra smart, like Class or Style, which will SmartJoin the values if called multiple times. Read the special tutorial for that. - Set Tag-Specific Attributes
All known html5 objects are supported, and return specially typed objects. So you can write@Tag.Img().Src("...")
or@Tag.A().Href(...)
, butwon't work.@Tag.Img().Href(...)
Important: Some attributes are extra smart, likeSrc
onImg
orSrcset
onSource
. Read the special tutorial for that.
RazorBlade Id
, Title
, Class(...)
and Style(...)
with SmartJoin
All html attributes can have these Properties
id
- made with.Id(...)
(replaces previous id)class
- made with.Class(...)
(expands previous class)style
- made with.Style(...)
(expands previous style)title
- made with.Title(...)
(replaces previous title)
So Tag
objects have quick commands to set these. What makes it magical is that they always return the main object again, so you can chain them like@Tag.Div().Id("wrapper").Class("alert").Class("alert-primary)
Info: SmartJoin
Note that some of these, like Id
will replace the previous value. Others like Class
will add new values to the attribute.
Imagine your code will add attributes step by step by using some kind of logic. In situations where you add more classes, they should be appended - like firstClass secondClass
. Others should be appended with a special character like ;
in styles because you want width: 75px; height: 25px
. And others should replace the previous value - like id
should always only have one value.
RazorBlade Safe Attributes API @Tag.Attr v3
Here we'll go through some examples using @Tag.Attr(...)