我正在使用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] 删除。
我来说两句