用c#中的其他文本替换变量文本(replacing a variable text with other text in c#)

编程入门 行业动态 更新时间:2024-10-27 02:20:10
用c#中的其他文本替换变量文本(replacing a variable text with other text in c#)

我使用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.

更多推荐

本文发布于:2023-08-05 19:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1438077.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   变量   replacing   variable   text

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!