Use Koi to Adjust to the Page CSS Framework
Learn how to use Connect.Koi
to ensure your module/template has the right CSS frameworks, to either auto-include otherwise, show messages to the admin or generate different output. This uses Connect.Koi.
Some notes before we start
Koi works like magic behind the scenes, ensuring that your component knows what CSS-framework is already included, and can adapt its behavior.
For this to work, the theme must communicate what framework it's using, by including a koi.json
file as explained in the docs. This is included in the default DNN themes since DNN 9.2, as well as in Bootstrap Instant, nvQuickTheme and many other.
Some themes may not include that file, so your best bet is to always have something which either alerts the user, that this is missing, or automatically adds your framework, if it can't detect what's happening.
In this example, we'll assume your template needs Bootstrap5 as the preferred CSS framework. Note that this works for other frameworks as well, with minor modifications. But if it's used on any page, you may have one of the following situations:
- Bootstrap5 could already be included - so you shouldn't do anything
- A different CSS framework is included - so you should include Bootstrap5 automatically
- The current CSS framework can't be detected, so to be safe, you should include Bootstrap 5 as well, until the theme is updated to broadcast what it's using
This page shows how to handle these problems with almost no code. Note that we've place the code in a separate file and use a helper to achieve this, as this allows us to re-use the code in multiple templates.
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.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.In this example, we'll assume you have a Bootstrap3 and a Bootstrap4 template in the folders bs3/_Line.cshtml
and the same in bs4/_Line.cshtml
. We'll use Koi to load the right template file.
You can see in the source-codes that BS3 uses pull-left
and another class on the blockquote tag than BS4.
Result
You are seeing the Bootstrap5 Template
"this is pretty awesome!"