In this example, we'll assume your template needs Bootstrap5 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.
-
-
Result
The result is invisible; bootstrap5 is auto-added if not already present.
@inherits Custom.Hybrid.Razor14
@{
var bsCheck = CreateInstance("../../shared/Bootstrap5.cs");
bsCheck.EnsureBootstrap5();
}
@bsCheck.WarnAboutMissingOrUnknownBootstrap()
Source Code of Bootstrap5.cs
using ToSic.Razor.Blade;
public class Bootstrap5 : Custom.Hybrid.CodeTyped
{
// 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 EnsureBootstrap5()
{
if (Kit.Css.IsUnknown) {
Kit.Page.Activate("Bootstrap5");
}
}
// show warning for admin if koi.json is missing
public IHtmlTag WarnAboutMissingOrUnknownBootstrap() {
if (Kit.Css.IsUnknown && MyUser.IsSiteAdmin) {
return Tag.Div().Class("alert alert-warning").Wrap(
Connect.Koi.Messages.CssInformationMissing,
Tag.Br(),
Connect.Koi.Messages.OnlyAdminsSeeThis
);
}
return null;
}
}