Extension Methods
ZauberCMS provides extension methods to simplify common data access patterns. These extensions work with the service-based architecture and are available throughout your components.
Content Extensions
These extension methods work on any object implementing IHasPropertyValues
(like Content
). Services must be injected via @inject
directives in your components.
Get Multiple Media Items
/// <summary>
/// Retrieves a list of media items from a content property.
/// </summary>
/// <param name="content">The content item that holds the media property.</param>
/// <param name="alias">The alias of the media property to fetch.</param>
/// <param name="mediaService">The media service to query.</param>
/// <param name="fallBackUrl">A fallback URL to use if no media is found.</param>
/// <returns>List of media items if found; otherwise, a list containing a single media item with the fallback URL.</returns>
public static async Task<List<Media.Models.Media>> GetMediaItems(
this IHasPropertyValues content,
string? alias,
IMediaService mediaService,
string? fallBackUrl = null)
Usage:
var images = await Content!.GetMediaItems("Gallery", MediaService, "/default.jpg");
Get Single Media Item
/// <summary>
/// Extension to get a single media item
/// </summary>
/// <param name="content">The content containing media data</param>
/// <param name="propertyAlias">The property alias to retrieve media ids</param>
/// <param name="mediaService">The media service to handle media queries</param>
/// <param name="fallBackUrl">Optional fallback URL</param>
/// <returns>A single media item or null if no media items are found</returns>
public static async Task<Media.Models.Media?> GetMedia(
this IHasPropertyValues content,
string propertyAlias,
IMediaService mediaService,
string? fallBackUrl = null)
Usage:
var headerImage = await Content!.GetMedia("HeaderImage", MediaService, "/default.jpg");
Get Multiple Content Items
/// <summary>
/// Extension to get content items from a picker
/// </summary>
/// <param name="content">The content object</param>
/// <param name="propertyAlias">The property alias</param>
/// <param name="contentService">The content service</param>
/// <returns>List of content items</returns>
public static async Task<List<Content.Models.Content>> GetContentItems(
this IHasPropertyValues content,
string? propertyAlias,
IContentService contentService)
Usage:
var relatedPages = await Content!.GetContentItems("RelatedPages", ContentService);
Get Single Content Item
/// <summary>
/// Extension to get a single content item
/// </summary>
/// <param name="content">The content containing content data</param>
/// <param name="propertyAlias">The property alias to retrieve content ids</param>
/// <param name="contentService">The content service to handle content queries</param>
/// <returns>A single content item or null if no content items are found</returns>
public static async Task<Content.Models.Content?> GetContent(
this IHasPropertyValues content,
string propertyAlias,
IContentService contentService)
Usage:
var linkedPage = await Content!.GetContent("RelatedPage", ContentService);
Get Content Blocks
/// <summary>
/// Retrieves a collection of content blocks based on the specified alias.
/// </summary>
/// <param name="content">The content instance implementing IHasPropertyValues.</param>
/// <param name="alias">The alias of the property containing the block IDs.</param>
/// <param name="contentService">The service for querying content blocks.</param>
/// <returns>A collection of content blocks.</returns>
public static async Task<IEnumerable<Content.Models.Content>> GetBlocks(
this IHasPropertyValues content,
string alias,
IContentService contentService)
Usage:
var contentBlocks = await Content!.GetBlocks("ContentBlocks", ContentService);
Get Multiple Users
/// <summary>
/// Extension to get users from a user picker
/// </summary>
/// <param name="content">The content object</param>
/// <param name="propertyAlias">The property alias</param>
/// <param name="membershipService">The membership service</param>
/// <returns>List of users</returns>
public static async Task<List<User>> GetUsers(
this IHasPropertyValues content,
string propertyAlias,
IMembershipService membershipService)
Usage:
var authors = await Content!.GetUsers("Authors", MembershipService);
Get Single User
/// <summary>
/// Extension to get a single user
/// </summary>
/// <param name="content">The content containing user data</param>
/// <param name="propertyAlias">The property alias to retrieve user ids</param>
/// <param name="membershipService">The membership service to handle user queries</param>
/// <returns>A single user or null if no users are found</returns>
public static async Task<User?> GetUser(
this IHasPropertyValues content,
string propertyAlias,
IMembershipService membershipService)
Usage:
var author = await Content!.GetUser("Author", MembershipService);
Service Extensions
These extension methods provide shortcuts for common service operations.
Get Global Settings
/// <summary>
/// Retrieves the global settings.
/// </summary>
/// <param name="dataService">The data service instance</param>
/// <returns>An instance of GlobalSettings.</returns>
public static async Task<GlobalSettings> GetGlobalSettings(this IDataService dataService)
Usage:
var settings = await DataService.GetGlobalSettings();
Save Global Settings
/// <summary>
/// Saves the global settings.
/// </summary>
/// <param name="dataService">The data service instance.</param>
/// <param name="settings">The global settings to be saved.</param>
/// <returns>Boolean indicating the success of the save operation.</returns>
public static async Task<bool> SaveGlobalSettings(
this IDataService dataService,
GlobalSettings settings)
Usage:
var success = await DataService.SaveGlobalSettings(mySettings);
Get Global Data (Generic)
/// <summary>
/// Retrieves global data using the specified alias.
/// </summary>
/// <param name="dataService">The data service instance.</param>
/// <param name="alias">The alias associated with the global data.</param>
/// <typeparam name="T">The type to which the global data should be converted.</typeparam>
/// <returns>An instance of the specified type.</returns>
public static async Task<T?> GetGlobalData<T>(this IDataService dataService, string alias)
Usage:
var myData = await DataService.GetGlobalData<MyCustomSettings>("MyCustomData");
Get Current User
/// <summary>
/// Gets the currently logged in user
/// </summary>
/// <param name="membershipService">The membership service</param>
/// <returns>Current user or null</returns>
public static async Task<User?> GetCurrentUser(this IMembershipService membershipService)
Usage:
var currentUser = await MembershipService.GetCurrentUser();
Get User by ID (Shorthand)
/// <summary>
/// Retrieves a user based on the specified ID.
/// </summary>
/// <param name="membershipService">The membership service instance.</param>
/// <param name="id">The unique identifier of the user to retrieve.</param>
/// <param name="cached">Indicates whether to use cached data. Default is true.</param>
/// <returns>An instance of User, or null if no user is found.</returns>
public static async Task<User?> GetUser(
this IMembershipService membershipService,
Guid? id,
bool cached = true)
Usage:
var user = await MembershipService.GetUser(userId);
Get Media by ID (Shorthand)
/// <summary>
/// Retrieves media with the specified ID.
/// </summary>
/// <param name="mediaService">The media service instance.</param>
/// <param name="id">The ID of the media to retrieve.</param>
/// <param name="cached">Whether to retrieve the cached version. Default is true.</param>
/// <returns>An instance of Media or null.</returns>
public static async Task<Media.Models.Media?> GetMedia(
this IMediaService mediaService,
Guid? id,
bool cached = true)
Usage:
var media = await MediaService.GetMedia(mediaId);
Get Content by ID (Shorthand)
/// <summary>
/// Retrieves content based on the provided content ID.
/// </summary>
/// <param name="contentService">The content service</param>
/// <param name="id">The unique identifier of the content to retrieve.</param>
/// <param name="cached">Indicates whether to use cached content data. Default is true.</param>
/// <returns>An instance of Content if found; otherwise, null.</returns>
public static async Task<Content.Models.Content?> GetContent(
this IContentService contentService,
Guid? id,
bool cached = true)
Usage:
var content = await ContentService.GetContent(contentId);
Complete Example
Here's a complete example showing multiple extension methods in use:
@page "/blog"
@using ZauberCMS.Core.Content.Parameters
@implements ZauberCMS.Core.Content.Interfaces.IContentView
@layout MainLayout
<header class="masthead" style="background-image: url('@HeaderImage?.Url')">
<h1>@Content!.GetValue<string>("Heading")</h1>
</header>
<div class="container">
@foreach (var block in ContentBlocks)
{
<RenderBlock Content="@block" />
}
@foreach (var post in BlogPosts)
{
<BlogPostPreview Content="@post" />
}
</div>
@code {
[Parameter] public Content? Content { get; set; }
[Parameter] public Dictionary<string, string>? LanguageKeys { get; set; }
private Media? HeaderImage { get; set; }
private IEnumerable<Content> ContentBlocks { get; set; } = [];
private IEnumerable<Content> BlogPosts { get; set; } = [];
protected override async Task OnInitializedAsync()
{
// Get single media with fallback
HeaderImage = await Content!.GetMedia("HeaderImage", MediaService, "/default.jpg");
// Get content blocks
ContentBlocks = await Content!.GetBlocks("ContentBlocks", ContentService);
// Get related content items
var featuredPosts = await Content!.GetContentItems("FeaturedPosts", ContentService);
// Query for blog posts
var posts = await ContentService.QueryContentAsync(new QueryContentParameters
{
ContentTypeAlias = "BlogPost",
AmountPerPage = 10,
OrderBy = GetContentsOrderBy.DateCreatedDescending,
Cached = true
});
BlogPosts = posts.Items;
// Get current user
var currentUser = await MembershipService.GetCurrentUser();
// Get global settings
var settings = await DataService.GetGlobalSettings();
}
}
Last updated