Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@davidknipe
Last active March 27, 2019 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidknipe/4dc89826ea00d4fc65df60965526b69d to your computer and use it in GitHub Desktop.
Save davidknipe/4dc89826ea00d4fc65df60965526b69d to your computer and use it in GitHub Desktop.
Tokenised Content in Episerver
using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.PlugIn;
namespace TokenisedContent.Features.TokenisedContent
{
[PropertyDefinitionTypePlugIn]
public class ReplacementTokenProperty : PropertyList<ReplacementToken> { }
public class ReplacementToken
{
public string Token { get; set; }
[Display(Name = "Replacement Text")]
public string ReplacementText { get; set; }
}
}
using System.Collections.Generic;
using EPiServer;
using EPiServer.Core;
using EPiServer.ServiceLocation;
namespace TokenisedContent.Features.TokenisedContent.Impl
{
// Example implmentation that pulls the list of tokens from a
// property called ReplacementTokens on the homepage of the site
[ServiceConfiguration(typeof(IReplacementTokens))]
public class ReplacementTokens : IReplacementTokens
{
private readonly IContentRepository _contentRepository;
public ReplacementTokens(IContentRepository contentRepository)
{
_contentRepository = contentRepository;
}
public IList<ReplacementToken> GetReplacementTokens()
{
var startPage = _contentRepository.Get<PageData>(ContentReference.StartPage);
if (startPage?.Property["ReplacementTokens"] != null)
{
if (startPage.Property["ReplacementTokens"].Value is IList<ReplacementToken> tokens)
{
return tokens;
}
}
return new List<ReplacementToken>();
}
}
public interface IReplacementTokens
{
IList<ReplacementToken> GetReplacementTokens();
}
}
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using TokenisedContent.Features.TokenisedContent;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.SpecializedProperties;
using TokenisedContent.Models.Blocks;
using EPiServer.Cms.Shell.UI.ObjectEditing.EditorDescriptors;
using EPiServer.Shell.ObjectEditing;
namespace TokenisedContent.Models.Pages
{
/// <summary>
/// Example to show how to add the property to the start page
/// </summary>
[ContentType(
GUID = "19671657-B684-4D95-A61F-8DD4FE60D559",
GroupName = Global.GroupNames.Specialized)]
public class StartPage : SitePageData
{
// << Other property definitions here >>
[Display(
GroupName = "Content tokens",
Name = "Content tokens",
Description = "Site wide replacement tokens")]
[EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor<ReplacementToken>))]
public virtual IList<ReplacementToken> ReplacementTokens { get; set; }
}
}
using System.Linq;
using EPiServer.ServiceLocation;
using EPiServer.Web.Routing;
// See https://github.com/RSuter/MyToolkit and https://www.nuget.org/packages/MyToolkit.AspNet.Mvc
using MyToolkit.Filters;
using TokenisedContent.Features.TokenisedContent.Interfaces;
namespace TokenisedContent.Features.TokenisedContent.Attributes
{
public class TokenReplacementAttribute : OutputProcessorActionFilterAttribute // See https://github.com/RSuter/MyToolkit and https://www.nuget.org/packages/MyToolkit.AspNet.Mvc
{
#pragma warning disable 649
private readonly Injected<IReplacementTokens> _replacementTokens;
private readonly Injected<IPageRouteHelper> _pageRouteHelper;
#pragma warning restore 649
protected override string Process(string data)
{
var currentPage = _pageRouteHelper.Service?.Page;
if (currentPage != null)
{
var replacementTokens = _replacementTokens.Service?.GetReplacementTokens();
if (replacementTokens?.Any() == true)
{
// Depending on the number of replacements there may be a case for optimisation here - send me a PR ;)
foreach (var token in replacementTokens)
{
data = data.Replace(token.Token, token.ReplacementText);
}
}
}
return data;
}
}
}
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
namespace TokenisedContent.Features.TokenisedContent
{
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class TokenReplacementInit : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
GlobalFilters.Filters.Add(ServiceLocator.Current.GetInstance<TokenReplacementAttribute>());
}
public void Uninitialize(InitializationEngine context) { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment