Often you may need context - like the Dnn
or App
objects. We made this easy by defining a base class you can inherit from, called Custom.Hybrid.Code14
(previously ToSic.Sxc.Dnn.DynamicCode
). If you use that as your base-class, all existing context is automatically attached, allowing you to access variables like App
.
-
-
@inherits Custom.Hybrid.RazorTyped
@{
var powerLib = GetCode("FunctionsWithContext.cs");
}
<div>
<img loading="lazy" src='@powerLib.QrPath("https://2sxc.org")' width="75px">
</div>
Source Code of FunctionsWithContext.cs
// Important notes:
// - This class should have the same name as the file it's in
// - This inherits from Custom.Hybrid.Code14
// which will automatically provide the common objects like App, Dnn etc.
// from the current context to use in your code
public class FunctionsWithContext: Custom.Hybrid.Code14 {
public string QrPath(string link) {
// path to qr-code generator
var qrPath = "//api.qrserver.com/v1/create-qr-code/?color={foreground}&bgcolor={background}&qzone=0&margin=0&size={dim}x{dim}&ecc={ecc}&data={link}"
.Replace("{foreground}", App.Settings.QrForegroundColor.Replace("#", ""))
.Replace("{background}", App.Settings.QrBackgroundColor.Replace("#", ""))
.Replace("{dim}", App.Settings.QrDimension.ToString())
.Replace("{ecc}", App.Settings.QrEcc)
.Replace("{link}", link)
;
return qrPath;
}
}