Created
April 24, 2022 19:03
-
-
Save AThraen/7ea2dbf98bcc947b0d87728ecc401e63 to your computer and use it in GitHub Desktop.
Optimizely CMS with Fluid property support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Located in Shared/DisplayTemplates | |
| @model String | |
| @inject LocalAlloy.Fluid.FluidHelper helper | |
| @Html.Raw(helper.Parse(Model)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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>(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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