Skip to content

Instantly share code, notes, and snippets.

@seangwright
Created January 21, 2021 18:52
Show Gist options
  • Save seangwright/5290ee1047a30e0916f85089cf58c2eb to your computer and use it in GitHub Desktop.
Save seangwright/5290ee1047a30e0916f85089cf58c2eb to your computer and use it in GitHub Desktop.
Reusable Xperience Email Transformations
using System;
using System.Collections.Generic;
using System.Linq;
using CMS;
using CMS.Helpers;
using CMS.MacroEngine;
using CMS.PortalEngine;
using CMS.SiteProvider;
using Sandbox.Infrastructure.Emails;
[assembly: RegisterExtension(typeof(EmailMacroMethodContainer), typeof(StringNamespace))]
namespace Sandbox.Infrastructure.Emails
{
public class EmailMacroMethodContainer : MacroMethodContainer
{
/// <summary>
/// Returns the live site domain
/// </summary>
/// <param name="_">Evaluation context with child resolver</param>
/// <param name="parameters">Method parameters</param>
[MacroMethod(typeof(string), "Returns live site domain", 0)]
public static object SiteLinkDomain(EvaluationContext _, params object[] parameters)
{
switch (parameters.Length)
{
case 0:
return SiteInfoProvider.GetSiteInfo(SiteContext.CurrentSiteID)?.SitePresentationURL ?? "";
default:
throw new NotSupportedException();
}
}
/// <summary>
/// Evaluates the provided transformation without a data context and returns the transformation content
/// with all macros resolved
/// </summary>
/// <param name="context"></param>
/// <param name="parameters"></param>
/// <returns></returns>
[MacroMethod(typeof(string), "Returns the provided transformation evaluated 1 time without a data context", 1)]
public static object ApplySimpleTransformation(EvaluationContext context, params object[] parameters)
{
switch (parameters.Length)
{
case 1:
string content = GetTransformationCode(GetStringParam(parameters[0], context.Culture));
var bodyResolver = context.Resolver.CreateChild();
return bodyResolver.ResolveMacros(content, context);
default:
throw new NotSupportedException();
}
}
private static string GetTransformationCode(string transformation)
{
// Try to load transformation code
var ti = TransformationInfoProvider.GetTransformation(transformation);
if (ti != null)
{
switch (ti.TransformationType)
{
case TransformationTypeEnum.Text:
case TransformationTypeEnum.Html:
// Return transformation code
return ti.TransformationCode;
default:
// Not supported transformation type
throw new NotSupportedException("[MacroMethods.ApplyTransformation]: Only text/html transformation is supported.");
}
}
// Return input text
return transformation;
}
}
}
{% ApplySimpleTransformation("Sandbox.Transformations.EmailCompanyHeader")|(encode)false #%}
<tr>
<td style="padding: 40px 20px;">
<p style="color: #000; margin: 8px 0 0; padding: 0;">
{% Greeting %}, <a href="{% SiteLinkDomain() %}" target="_blank">Click this link to visit our site</a>
</p>
</td>
</tr>
{% ApplySimpleTransformation("Sandbox.Transformations.EmailCompanyFooter")|(encode)false #%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment