Skip to main content

Razor Blade Tutorials

Tutorial HomeRazor.Blade

RazorBlade Safe Attributes API @Tag.Attr v3

Here we'll go through some examples using @Tag.Attr(...)

Creating Simple Attributes

This is a simple demo showing how to add the title and class attribute using code.
It switches between warning/primary depending of the second being odd or even. Refresh the page to see the difference.
If you mouse-over, you'll also see the tooltips showing stuff which would cause trouble otherwise.

Source Code of this file

Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials.

@inherits ToSic.Sxc.Dnn.RazorComponent
@using ToSic.Razor.Blade;
<!-- unimportant stuff, hidden -->
RazorBlade Safe Attributes API... <!-- unimportant stuff, hidden -->

Creating Simple Attributes
@{
  var titleWithIssues = "I said \"check this out!\" \n\n even 'different quotes' and line breaks work!";
  var cls = "alert alert-";
  cls += (DateTime.Now.Second % 2 == 0) ? "warning" : "primary";
}
<div @Tag.Attr("title", titleWithIssues) 
  @Tag.Attr("class", cls)
  @Tag.Attr("number", 27)>
  This is a simple demo showing how to add... <!-- unimportant stuff, hidden -->
</div>

<!-- unimportant stuff, hidden -->