我使用c#和sitecore在某些地方基本上使用令牌(请参阅: 如何在sitecore中创建自定义令牌 )。 我想我有一个解决方案,但我不确定为什么它不起作用,即使我没有错误。
Item tokenItem = Sitecore.Context.Database.Items["/sitecore/content/Site Content/Tokens"]; if (tokenItem.HasChildren) { var sValue = args.FieldValue.ToString(); foreach (Item child in tokenItem.Children) { if (child.Template.Name == "Token") { string home = child.Fields["Title"].Value; string hContent = child.Fields["Content"].Value; if (sValue.Contains(home)) { home.Replace(home, hContent); } } } }home和hContent拉出每个容器的正确值,但是当页面加载时,它仍然在内容区域输入“home”值(即:@@ sales)而不是新值,该值存储在hContent中。 sValue包含所有内容(表格,div,文本),我试图挑出一个等于“home”的值,并用hContent替换“home”值。 我错过了什么?
I am using c# and sitecore to basically use tokens in certain places ( see: how to create a custom token in sitecore ). I think I have a solution, but am not sure as to why it is not working, even though I am getting no errors.
Item tokenItem = Sitecore.Context.Database.Items["/sitecore/content/Site Content/Tokens"]; if (tokenItem.HasChildren) { var sValue = args.FieldValue.ToString(); foreach (Item child in tokenItem.Children) { if (child.Template.Name == "Token") { string home = child.Fields["Title"].Value; string hContent = child.Fields["Content"].Value; if (sValue.Contains(home)) { home.Replace(home, hContent); } } } }home and hContent pull up the correct values of each container, but when the page loads, it still has the "home" value inputted (the ie: @@sales) in the content area instead of the new value, which is stored in hContent. The sValue contains everything (tables, divs, text) and I was trying to single out a value that equals to "home" and replace the "home" value with hContent. What am I missing?
最满意答案
如果您的代码是作为RenderField管道的处理器实现的,那么您需要将工作结果放回到args中。 尝试这样的事情:
Item tokenItem = Sitecore.Context.Database.Items["/sitecore/content/Site Content/Tokens"]; if (tokenItem.HasChildren) { var sValue = args.Result.FirstPart; foreach (Item child in tokenItem.Children){ if (child.Template.Name == "Token") { string home = child.Fields["Title"].Value; string hContent = child.Fields["Content"].Value; if (sValue.Contains(home)) { sValue = sValue.Replace(home, hContent); } } } args.Result.FirstPart = sValue; }请注意,您需要确保在GetFieldValue处理器之后将此处理器修补到管道中。 该处理器负责将字段值拉入args.Result.FirstPart 。
If your code is implemented as a processor for the RenderField pipeline, you need to put the result of your work back into args. Try something like this:
Item tokenItem = Sitecore.Context.Database.Items["/sitecore/content/Site Content/Tokens"]; if (tokenItem.HasChildren) { var sValue = args.Result.FirstPart; foreach (Item child in tokenItem.Children){ if (child.Template.Name == "Token") { string home = child.Fields["Title"].Value; string hContent = child.Fields["Content"].Value; if (sValue.Contains(home)) { sValue = sValue.Replace(home, hContent); } } } args.Result.FirstPart = sValue; }Note that you need to be sure to patch this processor into the pipeline after the GetFieldValue processor. That processor is responsible for pulling the field value into args.Result.FirstPart.
更多推荐
发布评论