您可以在 renderField 管道中添加一个步骤:
<renderField>
<processor type="Sitecore.Pipelines.RenderField.SetParameters, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.RenderField.GetFieldValue, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.RenderField.GetTextFieldValue, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.RenderField.ExpandLinks, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.RenderField.GetImageFieldValue, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.RenderField.GetLinkFieldValue, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.RenderField.GetInternalLinkFieldValue, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.RenderField.GetMemoFieldValue, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.RenderField.GetDateFieldValue, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.RenderField.GetDocxFieldValue, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.RenderField.AddBeforeAndAfterValues, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.RenderField.RenderWebEditing, Sitecore.Kernel"/>
<processor type="MyProject.ExpandNVelocityTokens, MyProject"/>
</renderField>
代码可能如下所示:
public class ExpandNVelocityTokens
{
public virtual void Process(RenderFieldArgs args)
{
if (!ShouldRun())
return;
if (!Sitecore.Context.PageMode.IsPageEditorEditing)
{
args.Result.FirstPart = ExpandVelocityTokens(args.Result.FirstPart);
args.Result.LastPart = ExpandVelocityTokens(args.Result.LastPart);
}
}
protected bool ShouldRun()
{
// In the cheapest possible way - determine if we need to do anything
}
protected string ExpandVelocityTokens(string input)
{
//... do velocity stuff here
}
}
注意:此管道针对渲染的每个字段运行,因此它非常快是至关重要的——因此,ShouldRun 方法可以尽早中断。除非必须,否则不要在这里做任何昂贵的事情。