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 an IScrub without having to use GetService<IScrub>

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<

Initial Code

The following code runs at the beginning and creates some variables/services used in the following samples.

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

After IScrub.All

Output

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

Output

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)

Output

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)

Output

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 @Sys.PageParts.InfoWrapper()>
  @Html.Partial("../shared/DefaultInfoSection.cshtml")
  <div @Sys.PageParts.InfoIntro()>
      <h2><em>RazorBlade</em> IScrub.All(...), IScrub.Only(), IScrub.Except()</h2>
      <p>
        These demos show how to strip all html from a string. You often need this in combination with @Sys.TutLink("crop or ellipsis", "blade110") to create teasers.
      </p>
  </div>
</div>


@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>

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



<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>



@* Footer *@
@Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })