如何不使用Sitecore Glass Mapper渲染链接标题

选修的

我正在使用GlassMapper的这种方法渲染Sitecore链接:

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

但不想在“编辑”模式下显示“链接说明”

因此,即使填写了链接说明,它也会呈现如下所示:

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

而不是这样:

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

所以我想知道是否可以为此类目的重写GlassHtml.RenderLink?特纳克斯

恩斯戈切夫

选项之一是,如果页面处于编辑模式,则为内容添加空白。它不会使链接为空,但不会显示其描述。

<% 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>
    <%}%>

另一种选择是编写自己的玻璃扩展名。(有关如何执行此类操作的更多信息,您可以看到此线程-Glass Mapper RenderLink链接描述-默认文本,如果为空

Glass.Mapper是开源的,您可以在此处查看渲染链接的实际工作方式:

https://github.com/mikeedwards83/Glass.Mapper/blob/master/Source/Glass.Mapper.Sc/GlassHtml.cs(该方法从第297行开始)。

要扩展它,您将需要这样的东西

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();

        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Glass Mapper在Sitecore视图渲染中使用通用类型

Sitecore Glass Mapper-使用项目渲染

Sitecore Glass.Mapper返回null

清除使用Glass.Mapper.Sc BeginRenderLink呈现的Sitecore链接字段可删除包装的字段

如何防止Glass Mapper进行Sitecore快速查询?

使用Glass Mapper的Sitecore 8可编辑字段

Sitecore + Glass:呈现链接,但忽略权限

如何在Sitecore 7.x和Solr 4.7中使用Glass Mapper 3.3

从Glass.Mapper.Sitecore升级到Glass.Mapper.Sc时缺少InstanceContext

对于Glass Mapper,如果在“常规链接”字段中选择了“内部链接”,则Glass Mapper使用项目名称作为链接文本

Glass Mapper渲染图像数据属性

Sitecore Glass Mapper获取所有兄弟姐妹

如何使用glass.mapper获取项目的模板ID?

在Sitecore中,Glass Mapper Editable功能在遍历对象集合时如何工作?

使用Glass Mapper对子代类型使用流利的映射

Glass Mapper RenderLink链接描述-默认文本(如果为空)

Glass.Mapper.Sc是否将索引字段用于sitecore7搜索

Sitecore中的A / B测试-Glass Mapper中的空数据源错误

如何将标准值应用于使用Glass.Mapper创建的项目

Glass Mapper V4语言项回退,如何使其与Autofac一起使用?

如何使用Glass Mapper获取空值而不是空字符串?

使用Glass.Mapper将Droplist项目映射到复杂类型

为什么Glass Mapper返回空值?

如何不使用jinja2渲染整个字符串

无法解析类型名称:Glass.Mapper.Sc.Pipelines.Response.GetModel,Glass.Mapper.Sc

如何不使用双指针从链接列表中删除最后一个节点

如何覆盖Google Glass的相机按钮

如何在Google Glass中滚动WebView

使用GDK的Google Glass制作渐晕