valikvs
valikvs

Reputation: 60

How not to render link title with Sitecore Glass Mapper

I'm rendering Sitecore Link with this method of GlassMapper:

        <li>
            <%=GlassHtml.RenderLink(icon, x => x.Link, null, true, string.Empty) %>
        </li>

but don't want to display Link description in Editing mode,

so even if Link description is filled it will be rendered like this:

<a href='https://url.com' class='icon-facebook' target='_blank' ></a>

and not like this:

<a href='https://url.com' class='icon-facebook' target='_blank' >Link description</a>

So I wonder if GlassHtml.RenderLink can be override for such kind of purposes? Tnx

Upvotes: 1

Views: 4027

Answers (1)

nsgocev
nsgocev

Reputation: 4470

One of the options is to add blank space for contents if the page is in editing mode. It won`t make the link empty but it won't show it's description.

<% if (IsInEditingMode)
       { %>
      <li>
                <%=GlassHtml.RenderLink(icon, x => x.Link, isEditable: true, contents: " ") %>
            </li>
    <% } else {%>

          <li>
                <%=GlassHtml.RenderLink(icon, x => x.Link, null, true, string.Empty) %>
            </li>
    <%}%>

Another option is to write your own glass extension. (For more information on how to do something like this you can see this thread - Glass Mapper RenderLink link description - default text if empty)

Glass.Mapper is open source and you can see how render link actually works here:

https://github.com/mikeedwards83/Glass.Mapper/blob/master/Source/Glass.Mapper.Sc/GlassHtml.cs (The method starts on line 297).

To extend it you will need something like this

public virtual string RenderEmptyLinkInEditing<T>(T model, Expression<Func<T, object>> field, object attributes = null, bool isEditable = false, string contents = null)
        {
            NameValueCollection attrs = null;

            if (attributes is NameValueCollection)
            {
                attrs = attributes as NameValueCollection;
            }
            else
            {
                attrs = Utilities.GetPropertiesCollection(attributes, true);

            }

            var sb = new StringBuilder();
            var writer = new StringWriter(sb);

            RenderingResult result = null;
            if (IsInEditingMode && isEditable)
            {

                if (contents.IsNotNullOrEmpty())
                {
                    attrs.Add("haschildren", "true");
                }

                result = MakeEditable(
                    field,
                    null, 
                    model,  
                    attrs,
                    _context, SitecoreContext.Database, writer);

              //  if (contents.IsNotNullOrEmpty())
              //  {
              //      sb.Append(contents);
              //  }
            }
            else
            {
                result = BeginRenderLink(
                        field.Compile().Invoke(model) as Fields.Link, attrs, contents, writer
                    );
            }

            result.Dispose();
            writer.Flush();
            writer.Close();
            return sb.ToString();

        }

Upvotes: 3

Related Questions