#2 Use PageService.Activate Features like JS/CSS
Use PageService.Activate Features like JS/CSS
2sxc 12.04 helps with many advanced challenges like these:
- Make sure that certain scripts are only loaded once (like jQuery or FancyBox)
- Make sure that all apps use the same version of these scripts
- Starting things in the right order
Activate Feature with the Page.Activate (IPageService)
The following example will tell the page that fancybox4
is required - which will automatically apply a lightbox to the image below.
It uses the WebResources
as configured in the Settings. By default, fancybox4 is already defined, but you can overide this or create your own WebResource definitions.
⬇️ Result | Source ➡️
Click on the image to see Fancybox in action!
@inherits Custom.Hybrid.Razor14
@{
// Activate the feature Fancybox4
// even if called multiple times, it will only be activated once
Kit.Page.Activate("fancybox4");
Kit.Page.Activate("fancybox4");
var imgUrl = App.Path + "/tutorials/code-pageservice/demo-banner.png";
}
<p>
Click on the image to see Fancybox in action!
</p>
<a data-fancybox='gallery' href="@imgUrl">
<img loading="lazy" src="@Link.Image(imgUrl, width: 200, height: 200)">
</a>
#2 Use PageService.Activate Features like JS/CSS