![](/img/trans.png)
[英]How to you create a custom index in Sitecore 7 to index non Sitecore items (f. ex. Users)
[英]how to create a custom token in sitecore
我正在尝试创建一个自定义标记,如果用户在富文本编辑器字段中输入某些文本,该文本将显示一个值。 所以说用户在富文本编辑器字段中输入@@ tester,“今天月是@@ tester”。 我有c#试图在sitecore的任何富文本编辑器字段中找到@@ tester,如果它找到该标记,则在标题字段中找到与要替换文本的位置匹配的标记。 所以ie:
Item Name = token
Item ID = {06912058-6U9A-4BBF-BAE3-9306974EBE68}
Title = @@tester
Content (rich text editor) = February
我到目前为止:
namespace LonzaWeb.Pipeline.RenderField
{
public class AddToken
{
public void Process(RenderFieldArgs args)
{
Item currentItem = Sitecore.Context.Item;
if ((args.FieldTypeKey == "rich text") && currentItem.TemplateID.ToString() == "{06912058-6U9A-4BBF-BAE3-9306974EBE68}")
{
var tItem = currentItem.Fields["Title"].ToString();
if (tItem != null) {
Regex regex = new Regex(tItem);
Match match = regex.Match(tItem);
}
}
}
}
}
此外,我正在尝试使用RegEx模式匹配。 我已将此添加到webconfig文件中以确保它查找它。
扩展<renderField>
管道可能是一种更好的方法。
可以使用自己的Process方法提供新类型。 可以使用RenderFieldArgs.FieldTypeKey检查正在处理的字段的类型。 可以使用每个支持的标记的值更新FieldValue属性。
管道中的每个处理器都会将生成的args传递给下一个处理器。 我会在GetTextFieldValue之后添加这个补丁。
由于您要在Live网站上呈现富文本值,因此可以在“页面加载”上进行替换。 以下方法可以是集中式方法,因此任何需要Rich Text Replacement的其他子布局都可以使用它
public static string RichTextReplacer(Item currentItem, string fieldName)
{
var richTextValue = currentItem.Fields[fieldName].Value;
var getToken = Sitecore.Context.Database.Items.GetItem("path to your token item");
var sb = new StringBuilder(richTextValue);
sb.Replace(getToken.Fields["Title"].value, getToken.Fields.["Content"].value);
return sb.ToString();
}
您可以将该方法用作集中,只需传递所需的不同参数即可。 您可以添加/删除适合您需求的参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.