1

For VariantTemplate, I want the tag to be empty and just want the value from the template. Even when selecting the empty tag, it renders a div in the site. I also changed the standard values of VariantTemplate to change to an empty tag. Still doesn't work. Is there a way to get empty tags? I'm using SXA version 1.8 enter image description here

2

You can't do this out of the box. You need to patch the Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderTemplate.RenderField method. Out of the box, this method does this:

public override void RenderField(RenderVariantFieldArgs args)
{
  Sitecore.XA.Foundation.RenderingVariants.Fields.VariantTemplate variantField = args.VariantField as Sitecore.XA.Foundation.RenderingVariants.Fields.VariantTemplate;
  if (variantField == null)
    return;
  HtmlGenericControl tag = new HtmlGenericControl(string.IsNullOrWhiteSpace(variantField.Tag) ? "div" : variantField.Tag);
  this.AddClass(tag, variantField.CssClass);
  this.AddWrapperDataAttributes((RenderingVariantFieldBase) variantField, args, tag);
  Dictionary<string, object> parameters = new Dictionary<string, object>()
  {
    {
      "item",
      (object) args.Item
    }
  };
  if (args.Parameters != null && args.Parameters.ContainsKey("geospatial"))
    parameters.Add("geospatial", args.Parameters["geospatial"]);
  tag.InnerHtml = ServiceLocator.ServiceProvider.GetService<ITemplateRenderer>().ExecuteTemplate(args.Item.Name, variantField.Template, parameters);
  args.ResultControl = (Control) tag;
  args.Result = this.RenderControl(args.ResultControl);
}

Notice this line:

HtmlGenericControl tag = new HtmlGenericControl(string.IsNullOrWhiteSpace(variantField.Tag) ? "div" : variantField.Tag);

So if the tag field is empty, it defaults to a div. In my solution I replaced that method with:

public class RenderTemplate : Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderTemplate
{
    public override void RenderField(RenderVariantFieldArgs args)
    {
        var variantField = args.VariantField as VariantTemplate;
        if (variantField == null)
        {
            return;
        }

        if (!string.IsNullOrWhiteSpace(variantField.Tag))
        {
            base.RenderField(args);
        }
        else
        {
            var templateRenderer = ServiceLocator.ServiceProvider.GetService<ITemplateRenderer>();
            var parameters = new Dictionary<string, object>() { { "item", args.Item } };
            if (args.Parameters != null && args.Parameters.ContainsKey("geospatial"))
            {
                parameters.Add("geospatial", args.Parameters["geospatial"]);
            }

            // There is no surrounding html tag, so just render the resulting string from the template.
            args.Result = templateRenderer.ExecuteTemplate(args.Item.Name, variantField.Template, parameters);
        }
    }
}

Now, it keeps the empty tag field and just renders the contents out. Just patch that method instead of the default one, like this:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
    <sitecore>
        <pipelines>
            <renderVariantField>
                <processor type="MyNamespace.RenderTemplate, MyAssembly" resolve="true"
                        patch:instead="processor[@type='Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderTemplate, Sitecore.XA.Foundation.RenderingVariants']" />
            </renderVariantField>
        </pipelines>
    </sitecore>
</configuration>

For this patch file, its important that you place it in a z.Folder so that it patches after all the SXA include files that are in the App_Config/Include folder, otherwise the patch will fail.

| improve this answer | |
1

@Dawid Rutkowski, checked in SXA 1.9 - div tag is still there as fallback for template. @Richard Seal, approach you offered (skip Control creation) works fine except cases when VariantTemplate is under VariantSection - then template is not rendered at all - see in SXA 1.9 Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderSection :

base.PipelineManager.Run("renderVariantField", renderVariantFieldArg);
if (renderVariantFieldArg.ResultControl == null)
{
    continue;
}
control.Controls.Add(renderVariantFieldArg.ResultControl);

as Control is not created for VariantTemplate, Result is not added to a page.

| improve this answer | |
0

As far as I remember from the code in 1.8 this is how it works. We had fallback there, so even if you will leave this field empty it will render div anyway.

Good news is that we've changed this behaviour. In 1.9 you will be able to skip the wrapping tag (I'm not sure if this is already in 1.8.1).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.