You could add a step in the renderField pipeline:
<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>
The code could look like this:
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
}
}
NOTE: This pipeline is run for every field rendered so it is paramount that it is very fast - hence the ShouldRun method for breaking out early. Do not do anything expensive here unless you have to.