Skip to content

Latest commit

 

History

History
86 lines (60 loc) · 3.25 KB

index.md

File metadata and controls

86 lines (60 loc) · 3.25 KB

API Docs of RazorBlade 3.0

These docs are meant for programmers using RazorBlade. These will usually be people working with C# and Razor, MVC etc.

APIs you're probably looking for

  1. APIs for Text with commands like
    Crop(...), Ellipsis(...), First(...), Has(...), Zip(...)
  2. API for Tags with commands like
    1. Text to HTML conversions: Br2Nl(...), Br2Space(...), Decode(...), Encode(...), Nl2Br(...), ...
    2. Html generators: Attribute(...), Attributes(...), Tag(...)
  3. API for Tag with all known html5 tags like
    A(...), Abbr(...), Address(...), Area(...) all the way to Wbr(...)
  4. API for HtmlPage with
  5. Page properties (get/set): Description, Keywords, Title, ...
  6. Methods to add headers: AddIcon(...), AddIconSet(...), AddJsonLd(...), AddMeta(...), ...

Tutorials & Blogs

  1. An explanation of the Fluent Tag API
  2. We have an extensive tutorial on 2sxc for using RazorBlade
  3. We also have many blog posts on RazorBlade

What is RazorBlade 3?

A library of common functions for Razor, to lighten Razor templates and make work easier. Some examples:

You need to change the page title and some headers from a razor template:

@using ToSic.Razor.Blade;
HtmlPage.Title = "Title changed using Razor Blade! original";
HtmlPage.Description = "Learn to use Razor Blade " + HtmlPage.Description;
HtmlPage.Keywords = "Tutorial, Razor, Blade" + HtmlPage.Keywords;
HtmlPage.AddMeta("somename", "somevalue");
HtmlPage.AddIcon("iconfile.png");

You need the first 100 characters followed by an ellipsis (if truncated), but umlauts like ü will mess up your count or might even be cut off. This is automatically handled by:

  @* just cut it off at the visible character count, not splitting words *@
  @Text.Crop(someText, 100)

  @* truncate a text and if necessary, add ellipsis character *@
  @Text.Ellipsis(longText, 100)

  @* now the same thing, with text having html-tags *@
  @Text.Ellipsis(Tags.Strip(longText), 100)

You need a value, but if it's empty (null, spaces, line-breaks,   etc.), you need another one:

  @* Do this *@
  @Text.First(firstName, "nothing found");

  @* instead of this *@
  @if(String.IsNullOrWhiteSpace(firstName as string)) {
    @"nothing found"
  } else {
    @firstName
  }

Note that HTML whitespace like   will also be treated as empty, unless you add false as a last parameter. But RazorBlade does more than just skip empty texts, here some more examples:

  @* First non-empty of many possible values *@
  @Text.First(nameFromDb, nameFromProfile, defaultNameForThisCountry, "unknown")

  @* remove html from a wysiwyg-string *@
  @Tags.Strip(formattedText)

  @* the same with a custom ending *@
  @Text.Ellipsis(longText, 100, "...more")

  @* an it won't cut off in the middle of ä *@
  @Text.Ellipsis("Visit München", 10)