6

Is there a way to easily extend rendering variants logic?

My goal is to display the name of a parent item. Let's imagine that I'm using a Page List rendering and beside values from current context item fields I would like also to display the name of a parent item (the name can, for example, indicate my custom category name of something).

Is there a way to achieve this?

1 Answer 1

8

Yes, this is possible. There is an easy way to extend/add logic which is missing by implementing custom Variant Token.

This is an example implementation of ResolveParentName processor:

public class ResolveParentName : ResolveVariantTokensProcessor { public override string Token => "$parentName"; public override void ResolveToken(ResolveVariantTokensArgs args) { // this if statement is quite important in order to support this token in JSON Variants if (args.ResultControl != null) { args.ResultControl.Controls.Add(new LiteralControl { Text = args.ContextItem.Parent.Name }); } else { args.Result = args.ContextItem.Parent.Name; } } } 

Do not forget about registering this processor:

<pipelines> <resolveVariantTokens> <processor type="YOUR_NAMESPAVE.ResolveParentName, YOUR_ASSEMBLY" resolve="true" /> </resolveVariantTokens> </pipelines> 

Here is an example usage:

enter image description here

As you can see this is a great place to add a lot of custom logic and extend Rendering Variants. In the above example, I'm just using one args.ContextItem.Parent.Name property but you can do whatever you need there.

UPDATE: Please use lowercased token names (instead of $parentName go with $parentname)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.