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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top