2

I have a FieldRenderer control on my web page which is bound to a Sitecore item.

fieldRenderer.Item = SomeSitecoreItem;
fieldRenderer.FieldName = "SomeField";

Now SomeField within my SomeSitecoreItem has NVelocity tokens. How can I have fieldRenderer to render those NVelocity tokens.

For example the content of SomeField could be the following markup:

<h1>$!SomeToken</h1>

Is there a way to render $!SomeToken to be replaced by the corresponding value from code behind?

Ben
  • 538
  • 1
  • 9
  • 24
  • Take a look at [this blog post](http://www.matthewkenny.com/2013/11/nvelocity-sitecores-original-template-engine/) that does this same thing. There is also more info on token substitution in the [Reusing and Sharing Data](http://sdn.sitecore.net/upload/sitecore6/64/reusing_and_sharing_data-usletter.pdf) guide. – jammykam Apr 29 '14 at 06:50
  • Yes. Token substitution is exactly what I need. As explained in the answer. Thanks. – Ben Apr 29 '14 at 19:43

1 Answers1

3

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.

herskinduk
  • 1,187
  • 6
  • 14