A library of common functions for Razor, to lighten Razor templates and make work easier.
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)
You can use Razor Blade with any asp.net project, just by including the DLLs, or if you're using DNN, you can install the extension from the releases on Github. Note that if you are have 2sxc 10.9+ installed, it will automatically also install RazorBlade 3.
In your c# code, add the following line to then have access to all the commands:
@using ToSic.Razor.Blade;
This is a short summary of the most used variations of the helpers. Further details and syntaxes are listed further down.
-
Tags - see see Tags API docs
Tags.Br2Nl(text)
,Tags.Br2Space(text)
,Tags.Nl2Br(text)
,
Tags.Strip(text)
,
Tags.Encode(...)
,Tags.Decode(...)
etc. -
Tag - see Fluent API and Tag API docs
containsTag.A(...)
up toTag.Wbr(...)
(all knows Html5 tags),Tag.Tag(...)
for all custom tags andTag.Attribute(...)
-
Text - see API docs
Text.Crop(string, length)
,Text.Ellipsis(value, length)
,
Text.Has(value)
,Text.First(value, value[, moreValues, ...])
,Text.Zip(value)
, ... -
HtmlPage - see detailed docs
- get/set
HtmlPage.Title
,HtmlPage.Description
,HtmlPage.Keywords
HtmlPage.AddBase()
,HtmlPage.AddBase(url)
- set automatic base tagHtmlPage.AddMeta(name, content)
add a meta-tag to the headerHtmlPage.AddJsonLd(string|object)
create a Json-LD header see also google guidelineHtmlPage.AddOpenGraph(property, content)
add an open-graph tag to the header for facebook, twitter and co.HtmlPage.AddToHead(tagString)
add any tag string into the page<head>
sectionHtmlPage.AddToHead(tagObject)
add any tag object into the page<head>
section v2.1HtmlPage.AddIcon(path)
add an icon (favicon) to the page v2.1HtmlPage.AddIconSet(path)
add a few icon headers to the page according to best practices v2.1GetPage()
get an object with the same commands as HtmlPage, but a bit faster v2.2
- get/set
- Url
- SeoFragment(string) - in discussion, would take a string and save-convert it so it can be added to a url for SEO.
- AddParameters(...) - would add more url-parameters, and ensure that it only has one ? etc.
Tags.Strip(htmlText, tagName)
Tags.Strip(htmlText, csvListOfTagsToStrip)
Tags.Wrap(tagName, content, id, cls, attr)
- something which puts a tag (like a div, span, p) around some contentTags.Replace(htmlText, listOfTags, replacementTag)
- (place other wishes into issues for discussion)
Dic.ToDynamic(dictionary)
- converts a Dictionary to an expando object, so you can writeobj.Property
instead ofobj["Property"]
; would return null if a property would not be found.Mail.Generate(pathToRazor, objValues)
- uses a razor template to generate a mail.
Here you can also find scrapped ideas.
- Any tests and bugfixes are always welcome and will be processed quickly by iJungleboy.
- New commands / overloads / features should be discussed in issues before adding to this library, to ensure that it's inline with the overal purpose of this library.
Please also read the conventions so we can work on this together.
👉🏽 See Change Log