Skip to main content
Home  › ... Razor

Razor Blade Tutorials

Tutorial HomeRazor.Blade

RazorBlade IScrub.All(...), IScrub.Only(), IScrub.Except()

These demos show how to strip all html from a string. You often need this in combination with crop or ellipsis to create teasers.

Info about the Base Class

This tutorial inherits from the Custom.Hybrid.Razor14 base class.

This allows us to use Kit.Scrub to access IScrub without having to use GetService<>

Simple example

Original

<h1>Introduction</h1><p>Welcome to this blog post</p><hr><p>beginning with xyz we'll tell you more</p>
which if cropped at 50 would be really messy
<h1>Introduction</h1><p>Welcome to this blog post<

After IScrub.All

Introduction Welcome to this blog post beginning with xyz we'll tell you more

<code>@Kit.Scrub.All(val1)</code>

Usually you will then combine with crop or ellipsis

Introduction Welcome to this blog post beginning…

<code>@Html.Raw(Text.Ellipsis(Kit.Scrub.All(val1), cropLen))</code>

IScrub.Only()

Only scrub specified tags using IScrub.Only(source, tag)

Scrub one tag: <h2><span>The cool title</span></h2>
Scrub multiple tags: <span>The cool title</span>

@{
  var exampleTagsOnly = "<div><h2><span>The cool title</span></h2></div>"; 
}
<div><strong>Scrub one tag: </strong><code>@Kit.Scrub.Only(exampleTagsOnly, "div")</code></div>
<div><strong>Scrub multiple tags: </strong><code>@Kit.Scrub.Only(exampleTagsOnly, new string[] { "div", "h2" })</code></div>

IScrub.Except()

Scrub except the specified tags using IScrub.Except(source, tag)

Scrub tags except one tag: <h2>The cool title</h2>
Scrub tags except multiple tags: <div><span>The cool title</span></div>

@{
  var exampleTagsExcept = "<div><h2><span>The cool title</span></h2></div>"; 
}
<div><strong>Scrub tags except one tag: </strong><code>@Kit.Scrub.Except(exampleTagsExcept, "h2")</code></div>
<div><strong>Scrub tags except multiple tags: </strong><code>@Kit.Scrub.Except(exampleTagsExcept, new string[] { "div", "span" })</code></div>

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. Click to expand the code

@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade;
<!-- unimportant stuff, hidden -->
<div class="row">
  <div class="col-md-7">
    RazorBlade IScrub.All(...)... <!-- unimportant stuff, hidden -->
  </div>
  @Html.Partial("../shared/_DefaultInfoSection.cshtml")
</div>

@{
  var val1 = "<h1>Introduction</h1><p>Welcome to this blog post</p><hr><p>beginning with xyz we'll tell you more</p>";
  var cropLen = 50;
}

@Html.Partial("../shared/_KitBaseClassInfoBox.cshtml", new { ServiceName = "Scrub", Service = "IScrub" })

<h2>Simple example</h2>
<h3>Original</h3>
<code>@val1</code>
<div>which if cropped at @cropLen would be really messy</div>
<code>@val1.Substring(0, cropLen)</code>

<h3>After IScrub.All</h3>

<code>@Kit.Scrub.All(val1)</code>



<h3>Usually you will then combine with crop or ellipsis</h3>

<code>@Html.Raw(Text.Ellipsis(Kit.Scrub.All(val1), cropLen))</code>



<h3>IScrub.Only()</h3>
<p>Only scrub specified tags using <code>IScrub.Only(source, tag)</code></p>

@{
  var exampleTagsOnly = "<div><h2><span>The cool title</span></h2></div>"; 
}
<div><strong>Scrub one tag: </strong><code>@Kit.Scrub.Only(exampleTagsOnly, "div")</code></div>
<div><strong>Scrub multiple tags: </strong><code>@Kit.Scrub.Only(exampleTagsOnly, new string[] { "div", "h2" })</code></div>



<h3>IScrub.Except()</h3>
<p>Scrub except the specified tags using <code>IScrub.Except(source, tag)</code></p>

@{
  var exampleTagsExcept = "<div><h2><span>The cool title</span></h2></div>"; 
}
<div><strong>Scrub tags except one tag: </strong><code>@Kit.Scrub.Except(exampleTagsExcept, "h2")</code></div>
<div><strong>Scrub tags except multiple tags: </strong><code>@Kit.Scrub.Except(exampleTagsExcept, new string[] { "div", "span" })</code></div>



<!-- unimportant stuff, hidden -->