Skip to main content
Home  › ... Razor

Settings in 2sxc

Tutorial HomeSettings
#3 Use PageService.Activate to enable features, Css and Js

Settings in 2sxc 🆕 in 12.03

Settings allow your code to use predefined settings across all apps and sites. And they allow you to overide a setting at any level of the application. You can read more about the settings in the docs 📕.

Requirements
Resources

Use PageService.Activate to enable Features and Load JS/CSS

2sxc 12.04 helps with many advanced challenges like these:

  1. Make sure that certain scripts are only loaded once (like jQuery or FancyBox)
  2. Make sure that all apps use the same version of these scripts
  3. Starting things in the right order

Simple Example

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.

#3 Use PageService.Activate to enable features, Css and Js

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

@using ToSic.Sxc.Services;
@inherits Custom.Hybrid.Razor12
<!-- unimportant stuff, hidden -->
<hr>

Use PageService.Activate to enable... <!-- unimportant stuff, hidden -->

@{
  var pageService = GetService<IPageService>();
  pageService.Activate("fancybox4");
  pageService.Activate("fancybox4"); // even if called multiple times, it will only be activated once

  var imgUrl = App.Path + "/settings/demo-banner.png";
}

<a data-fancybox='gallery' href="@imgUrl">
  <img loading="lazy" src="@Link.Image(imgUrl, width: 200, height: 200)">
</a>


<!-- unimportant stuff, hidden -->