简体   繁体   English

如何在sitecore中创建自定义令牌

[英]how to create a custom token in sitecore

I am trying to create a custom token where, if a user inputs certain text in a rich text editor field, that text will display a value. 我正在尝试创建一个自定义标记,如果用户在富文本编辑器字段中输入某些文本,该文本将显示一个值。 So say the user inputs @@tester like, "Todays month is @@tester" in a rich text editor field. 所以说用户在富文本编辑器字段中输入@@ tester,“今天月是@@ tester”。 I have c# trying to find @@tester in any rich text editor field within sitecore, and if it finds that token to find that token in a title field that matches where the text is to be replaced. 我有c#试图在sitecore的任何富文本编辑器字段中找到@@ tester,如果它找到该标记,则在标题字段中找到与要替换文本的位置匹配的标记。 So ie: 所以ie:

Item Name = token
Item ID = {06912058-6U9A-4BBF-BAE3-9306974EBE68}
Title = @@tester
Content (rich text editor) = February

I have so far: 我到目前为止:

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

Also I am trying to use RegEx pattern matching. 此外,我正在尝试使用RegEx模式匹配。 I have added this into the webconfig file to make sure it looks for it. 我已将此添加到webconfig文件中以确保它查找它。

Extending the <renderField> pipeline is probably a better way to go. 扩展<renderField>管道可能是一种更好的方法。

A new type can be provided with its own Process method. 可以使用自己的Process方法提供新类型。 The type of the field being processed can be checked with the RenderFieldArgs.FieldTypeKey. 可以使用RenderFieldArgs.FieldTypeKey检查正在处理的字段的类型。 The FieldValue property can be updated with values for each supported token. 可以使用每个支持的标记的值更新FieldValue属性。

Each processor in the pipeline will pass the resulting args to the next processor in line. 管道中的每个处理器都会将生成的args传递给下一个处理器。 I would add this patch after GetTextFieldValue. 我会在GetTextFieldValue之后添加这个补丁。

http://www.sitecore.net/learn/blogs/technical-blogs/sitecorebetter/posts/2013/06/extending-the-sitecore-renderfield-pipeline.aspx http://www.sitecore.net/learn/blogs/technical-blogs/sitecorebetter/posts/2013/06/extending-the-sitecore-renderfield-pipeline.aspx

http://www.sitecore.net/learn/blogs/technical-blogs/john-west-sitecore-blog/posts/2011/08/replace-tokens-in-rich-text-fields-using-the-sitecore-aspnet-cms.aspx http://www.sitecore.net/learn/blogs/technical-blogs/john-west-sitecore-blog/posts/2011/08/replace-tokens-in-rich-text-fields-using-the-sitecore- ASPNET,cms.aspx

Since you are going to render the rich text value on the Live site, you can do the replacement on Page Load. 由于您要在Live网站上呈现富文本值,因此可以在“页面加载”上进行替换。 The below method can be a centralized one, so any other sublayouts that requires Rich Text Replacement can use it 以下方法可以是集中式方法,因此任何需要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();
}

You can use the method as centralize and you just need to pass the different parameters that are required. 您可以将该方法用作集中,只需传递所需的不同参数即可。 You may add / remove parameters that suit your needs 您可以添加/删除适合您需求的参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM