Skip to content

Instantly share code, notes, and snippets.

@AThraen
Created April 24, 2022 19:03
Show Gist options
  • Select an option

  • Save AThraen/7ea2dbf98bcc947b0d87728ecc401e63 to your computer and use it in GitHub Desktop.

Select an option

Save AThraen/7ea2dbf98bcc947b0d87728ecc401e63 to your computer and use it in GitHub Desktop.
Optimizely CMS with Fluid property support
using EPiServer.ServiceLocation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LocalAlloy.Fluid
{
public static class FluidExtensions
{
public static string ParseFluid(this string text)
{
return ServiceLocator.Current.GetInstance<FluidHelper>().Parse(text);
}
}
}
using EPiServer;
using EPiServer.Core;
using Fluid;
using LocalAlloy.Models.Pages;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LocalAlloy.Fluid
{
public class FluidHelper
{
private FluidParser _parser;
private TemplateContext _context;
public FluidHelper(UserContext userContext, IHttpContextAccessor httpContext, IContentRepository repo)
{
_parser = new FluidParser();
//Since this is registered as a Scoped service, we can create one context to use.
_context = new TemplateContext(userContext);
//Register other models to be available in Fluid, for example the HttpRequest:
_context.Options.MemberAccessStrategy.Register<HttpRequest>();
_context.SetValue("request", httpContext.HttpContext.Request);
//Or the start page with shared properties
_context.Options.MemberAccessStrategy.Register<StartPage>();
_context.SetValue("start", repo.Get<StartPage>(ContentReference.StartPage));
//TODO: Set other context settings or add other services
}
public string Parse(string input)
{
if (_parser.TryParse(input, out IFluidTemplate result))
{
return result.Render(_context);
} else return input;
}
}
}
//Located in Shared/DisplayTemplates
@model String
@inject LocalAlloy.Fluid.FluidHelper helper
@Html.Raw(helper.Parse(Model))
using EPiServer.Shell.ObjectEditing;
using EPiServer.Shell.ObjectEditing.EditorDescriptors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LocalAlloy.Fluid
{
[EditorDescriptorRegistration(TargetType = typeof(string), UIHint = "FluidString")]
public class FluidStringEditorDescriptor : StringEditorDescriptor
{
public FluidStringEditorDescriptor() {
this.ClientEditingClass = "dijit/form/Textarea";
}
}
}
//Only ConfigureServices included here:
public void ConfigureServices(IServiceCollection services)
{
if (_webHostingEnvironment.IsDevelopment())
{
AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(_webHostingEnvironment.ContentRootPath, "App_Data"));
services.Configure<SchedulerOptions>(options => options.Enabled = false);
}
services
.AddCmsAspNetIdentity<ApplicationUser>()
.AddHttpContextAccessor()
.AddCms()
//Add Fluid related services
.AddScoped<UserContext>()
.AddScoped<FluidHelper>()
//---
.AddAlloy()
.AddAdminUserRegistration()
.AddEmbeddedLocalization<Startup>();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LocalAlloy.Fluid
{
//Sample, hardcoded user context
public class UserContext
{
public string Name => "Allan";
public int Age => 42;
public string Location => "Denmark";
public string Company => "CodeArt";
public bool IsAdmin => true;
}
}
//In Shared/DisplayTemplates
@model XhtmlString
@inject LocalAlloy.Fluid.FluidHelper helper
@Html.Raw(helper.Parse(Model.ToHtmlString()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment