覆盖 sc:Image 替代文本
Override sc:Image alt text
我如何覆盖 Sitecore <sc:Image>
控件,以便它从同一项目的另一个字段读取替代文本?
我希望能给个标签,
<sc:Image contextAltText="fieldName" ...>
它应该首先查看这个字段,如果为空,它应该像往常一样呈现 Media Item Alt
文本。
我不需要详细的实施说明。我只需要知道我必须覆盖哪些 class 和函数。
您可以创建自己的 class 从 Sitecore.Web.UI.WebControls.Image
继承并像这样覆盖它:
namespace My.Assembly.Namespace
{
public class MyImage : Sitecore.Web.UI.WebControls.Image
{
public virtual string ContentAltText { get; set; }
protected override void PopulateParameters(Sitecore.Collections.SafeDictionary<string> parameters)
{
base.PopulateParameters(parameters);
if (!String.IsNullOrEmpty(Item[ContentAltText] ))
{
parameters.Add("alt", Item[ContentAltText]);
}
}
}
}
也许您需要替换原来的 alt
值而不是调用 parameters.Add
。我没有按照你的要求测试代码,它应该只为你指明了正确的方向。
编辑:下面的代码不适用于 ContentAltText
作为字段名称而不是不同的替代文本。
还有另一种方法,但我不知道这是否会覆盖默认的 alt
值,或者这只适用于新属性:
<sc:Image runat="server" Field="Field Name" Parameters="alt=OverridenAltValue" />
编辑 2:
要注册您的控件,请在 web.config
中找到下面的部分并添加带有自定义前缀的命名空间:
<system.web>
<pages>
<controls>
<add tagPrefix="sc" namespace="Sitecore.Web.UI.WebControls" assembly="Sitecore.Kernel"/>
<add tagPrefix="msc" namespace="My.Assembly.Namespace" assembly="My.Assembly"/>
...
</controls>
</pages>
</system.web>
您可能需要重新启动 Visual Studio 以确保它能识别新的命名空间。
然后使用:
<msc:ImageWithAlt runat="server" ... />
我如何覆盖 Sitecore <sc:Image>
控件,以便它从同一项目的另一个字段读取替代文本?
我希望能给个标签,
<sc:Image contextAltText="fieldName" ...>
它应该首先查看这个字段,如果为空,它应该像往常一样呈现 Media Item Alt
文本。
我不需要详细的实施说明。我只需要知道我必须覆盖哪些 class 和函数。
您可以创建自己的 class 从 Sitecore.Web.UI.WebControls.Image
继承并像这样覆盖它:
namespace My.Assembly.Namespace
{
public class MyImage : Sitecore.Web.UI.WebControls.Image
{
public virtual string ContentAltText { get; set; }
protected override void PopulateParameters(Sitecore.Collections.SafeDictionary<string> parameters)
{
base.PopulateParameters(parameters);
if (!String.IsNullOrEmpty(Item[ContentAltText] ))
{
parameters.Add("alt", Item[ContentAltText]);
}
}
}
}
也许您需要替换原来的 alt
值而不是调用 parameters.Add
。我没有按照你的要求测试代码,它应该只为你指明了正确的方向。
编辑:下面的代码不适用于 ContentAltText
作为字段名称而不是不同的替代文本。
还有另一种方法,但我不知道这是否会覆盖默认的 alt
值,或者这只适用于新属性:
<sc:Image runat="server" Field="Field Name" Parameters="alt=OverridenAltValue" />
编辑 2:
要注册您的控件,请在 web.config
中找到下面的部分并添加带有自定义前缀的命名空间:
<system.web>
<pages>
<controls>
<add tagPrefix="sc" namespace="Sitecore.Web.UI.WebControls" assembly="Sitecore.Kernel"/>
<add tagPrefix="msc" namespace="My.Assembly.Namespace" assembly="My.Assembly"/>
...
</controls>
</pages>
</system.web>
您可能需要重新启动 Visual Studio 以确保它能识别新的命名空间。
然后使用:
<msc:ImageWithAlt runat="server" ... />