Skip to main content

Koi Tutorials

Tutorial Home

Show Warning if Bootstrap4 not Included or Unclear

In this example, we'll assume your template needs Bootstrap4 as the preferred CSS framework. And we'll also assume, that you don't want to auto-include it, but instead want to warn the admin, to ensure he can correct the situation. This is what you want to do, when you believe the Admin should optimize the output, and prevent accidentally loading Bootstrap multiple times.

This page shows how to handle these problems with almost no code. We'll also show what you can do, so only admins see the message.
BTW: to see that this works, try switching the theme of this page to one without a koi.json or one with a different css-framework.

Requirements
Resources

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
<!-- unimportant stuff, hidden -->
Show Warning if Bootstrap4 not Included... <!-- unimportant stuff, hidden -->
@{
  var bsCheck = CreateInstance("../shared/Bootstrap4.cs");
  bsCheck.EnsureBootstrap4();
}
@bsCheck.WarnAboutMissingOrUnknownBootstrap()

<!-- unimportant stuff, hidden -->

Source Code of Bootstrap4.cs

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

using ToSic.Razor.Blade;

public class Bootstrap4 : Custom.Hybrid.Code14
{
  // if the theme framework is not BS4, just activate/load it from the WebResources
  // this solves both the cases where its unknown, or another framework
  public void EnsureBootstrap4()
  {
    if(Kit.Css.IsUnknown) {
      Kit.Page.Activate("Bootstrap4");
    }
  }

  // show warning for admin if koi.json is missing
  public dynamic WarnAboutMissingOrUnknownBootstrap() {
    if (Kit.Css.IsUnknown && CmsContext.User.IsSiteAdmin) {
      return Tag.Div().Class("dnnFormMessage dnnFormWarning").Wrap(
        Connect.Koi.Messages.CssInformationMissing,
        Tag.Br(),
        Connect.Koi.Messages.OnlyAdminsSeeThis
      );
    }
    return null;
  }
}