【问题标题】:Unable to understand VBScript set of code无法理解 VBScript 代码集
【发布时间】:2011-12-18 16:01:36
【问题描述】:

我需要将一些VBScript代码转换成C# 2.0,下面是VBScript代码,需要转换成c#。

' Component Template titles handled by the summary templates
FeaturedCT = "Featured Summary"
SummaryCT = "Summary"

' Set the looping variables on the first component presentation
If IsFirstComponent = 0 Then
    IsFirstComponent = 1

    ' Start out left handed by default
    IsLeftHand = True

    matchCount = 0
    Dim components()
    ReDim components(Page.ComponentPresentations.Count)

    ' Build a list of all the matching component presentations to be rendered out as summaries
    For Each objCP In Page.ComponentPresentations
        ' Is this a Summary component template?
        If objCP.ComponentTemplateTitle = SummaryCT Or objCP.ComponentTemplateTitle = FeaturedCT Then
            ' Check if this object should be included based on its approval status
            If staging Or getFieldValue(objCP.Component.MetadataFields("ApprovalStatus"), "") = "" Then
                If getFieldValue(objCP.Component.MetadataFields("EndDate"), "") <> "" Then
                    If getDateSerial(objCP.Component.MetadataFields("EndDate").Value(1), False) > getDateSerial(Now, False) Then
                        Set components(matchCount) = objCP
                        matchCount = matchCount + 1
                    End If
                Else
                    Set components(matchCount) = objCP
                    matchCount = matchCount + 1
                End If
            End If
        End If
    Next

    ' Resize the array to the amount of matches
    ReDim Preserve components(matchCount)
End If

For i = 0 to UBound(components) - 1
    ' Determine which component to render from the pre-selected array
    If components(i).ComponentID = Component.ID And components(i).OrdinalPosition = ComponentPresentation.OrdinalPosition Then
        ' Featured summary is always left aligned and causes all other items to be right-aligned
        If ComponentPresentation.ComponentTemplateTitle = FeaturedCT Then
            HasFeaturedSummary = 1
            IsLeftHand = True           
        End If

        Call RenderEntry(components, i)

        ' If a featured summary was previously present all following items are right-aligned
        If HasFeaturedSummary = 1 Then
            IsLeftHand = False
        Else
            ' Update the left-handed status
            UpdatePositioning
        End If

        If i = UBound(Components) - 1 Then
            WriteOut "<div class=""clearBoth""></div>"
        End If
    End If
Next

下面是我在 c# 中尝试的代码。

  public string RenderSummaryCT()
    {
        string FeaturedCT = "Featured Summary CT";
        string SummaryCT = "Summary CT";
        int IsFirstComponent = 0;
        string result = string.Empty;
        int hasFeaturedSummary = 0;
        Component comp = null;
        bool IsLeftHand = false;
        StringBuilder sbOutput = new StringBuilder();

        List<tc.ComponentPresentation> cmp = new List<tc.ComponentPresentation>();

        if (IsFirstComponent == 0)
        {
            IsFirstComponent = 1;
            IsLeftHand = true;

            //m_Logger.Info("CMP Array-" + cmp.Count);
            foreach (tc.ComponentPresentation objCMP in m_Page.ComponentPresentations)
            {
                if ((objCMP.ComponentTemplate.Title == SummaryCT) || (objCMP.ComponentTemplate.Title == FeaturedCT))
                {
                    comp = objCMP.Component;
                    string approvalStatus = string.Empty;
                    string endDate = string.Empty;

                    if (comp.Metadata != null)
                    {
                        ItemFields compItemfields = new ItemFields(comp.Metadata, comp.MetadataSchema);
                        approvalStatus = th.GetSingleStringValue("ApprovalStatus", compItemfields);
                        endDate = th.GetSingleStringValue("EndDate", compItemfields);
                    }
                    if ((baseutility.GetStagingConstantValue(m_Engine, m_Package)) || (string.IsNullOrEmpty(approvalStatus)))
                    {
                        if (!string.IsNullOrEmpty(endDate))
                        {
                            DateTime eDate = Convert.ToDateTime(baseutility.GetDateSerial(Convert.ToDateTime(endDate), false));
                            DateTime currentDate = Convert.ToDateTime(baseutility.GetDateSerial(DateTime.Now, false));
                            if ((eDate) > (currentDate))
                            {
                                cmp.Add(objCMP);
                            }
                        }
                        else
                        {
                            cmp.Add(objCMP);
                            //m_Logger.Info("2. Adding cmp: " + maxCount.ToString() + "-- " + cmp[maxCount].Component.Title);                              
                        }
                    }
                }
            }
        }
        bool lastFlag = false;
        int cnt = 0;
        int totalLength = cmp.Count;

        foreach (tc.ComponentPresentation cm in cmp)
        {
            m_Logger.Info(cm.Component.Id + "--" + m_Component.Id);

            m_Logger.Info(cnt + "--" + totalLength);

            if (cm.Component.Id == m_Component.Id)
            {
                if (cm.ComponentTemplate.Title == FeaturedCT)
                {
                    m_Logger.Info("inside featured CT");
                    hasFeaturedSummary = 1;
                    IsLeftHand = true;
                }
                sbOutput.Append("" + SummaryBase.SummaryHelper.RenderEntry(cmp, cnt, IsLeftHand, lastFlag));

                m_Logger.Info("IsLeftHand -: " + IsLeftHand.ToString());

                if (hasFeaturedSummary == 1)
                {
                    IsLeftHand = false;
                }
                else
                {
                    //sbOutput.Append("" + SummaryBase.SummaryHelper.UpdatePositioning(IsLeftHand));
                    if (IsLeftHand)
                    {
                        IsLeftHand = false;
                    }
                    else
                    {
                        //m_Logger.Info("UpdatePositioning");
                        sbOutput.Append("<div class=\"clearBoth\"></div>");
                        IsLeftHand = true;
                    }
                }

                m_Logger.Info("CMP Title -: " + cm.Component.Title);

                cnt = cnt + 1;
                if (totalLength == cnt)
                {
                    m_Logger.Info("cnt-" + cnt);
                    lastFlag = true;
                }
                if (lastFlag)
                {
                    sbOutput.Append(" <div class=\"clearBoth\"></div>");
                }
            }
        }

        return sbOutput.ToString();
    }

我确定有问题,请您建议使用上述 VBScript 逻辑的合适逻辑。

【问题讨论】:

  • 了解您认为错误的原因会很有帮助 - 您获得了错误的页面集,或者在错误的位置,或者您怀疑代码的哪一部分是错误的,等等。按原样您要求某人通过您的代码工作并为您验证整个事情。
  • @Rup,好吧,在这种情况下,我很抱歉,但你能检查一下“For i = 0 to UBound(components) - 1”下写的代码,只有我感到困惑..谢谢

标签: c# vbscript tridion


【解决方案1】:

这不仅仅是“将 VBScript 转换为 .NET”,您还可以从基于 COM 的 Tridion 对象模型迁移到 .NET TOM - 它的工作方式完全不同。

我的建议是对它采取非常分析的观点:

  1. 先分解代码,把逻辑写在纸上
  2. 尝试了解为什么会发生这种情况以及是否仍应如此

这应该使您以后重写代码变得容易得多。

我当然会借此机会重写它的一部分,这样它就更有意义了——比如将 IsFirstComponent 变量转换为布尔值而不是 int,并从你的 c# 代码中删除所有 HTML(不是你有很多,但下一个处理此代码的人可能想知道 HTML 不是由服务器上某处“隐藏”的某个程序集创建的)。

我稍后会尝试看一下这个 VBScript,但我认为此时真正了解代码的作用可能更重要。

【讨论】:

  • 感谢您的回复,感谢您的想法,问题出在时间上,我们的项目需要尽快与 .Net 模板同步,这是一个很大的实施,在我们的第一阶段实施中,我们仅针对到 .NET 的逻辑转换,在下一阶段将研究需要更改的内容。我希望这能让你清楚......再次感谢并继续发布你的好答案。
  • 知道了。不幸的是,在这种情况下,您不能只将 VBScript 转换为 .NET,因为 .NET 列表中没有“OrdinalPosition”。您可能需要遍历 Components 集合并检查 page.ComponentPresentations[i].Component.Id 是否与 Components[i].Id 匹配
  • 正如 Nuno 所说,在纸上写下这段代码的逻辑是个好主意。但是,我也会对所有主要的模板功能进行此练习。综合来看,这应该可以让您识别常见的模板代码片段并构建更好的模板解决方案(允许代码重用、更简单的调试等)
【解决方案2】:

说实话,我看不到ordinalPosition 正在做什么。我的猜测是,当您在每个组件表示上调用 render 时,Component 和 ComponentPresentation 的当前值都会更新。 (在旧式模板中,组件和页面共享相同的渲染堆栈。)但我在这里猜测,我仍然不确定预期的逻辑是什么。 @Nuno - 从“从页面中提取组件”中的 Page.ComponentPresentations 填充组件集合后,通常不会更新它,所以我怀疑黑魔法是否会如此轻松地移植。

所以 Manu - 您需要检查此功能的功能规格。这个想法可能只是简单地测试列表中的任何地方是否存在 Featured Summary 组件演示文稿,如果有,则将其放在左侧,其余放在右侧。 (如果是这样,那么到达那里的逻辑相当曲折。)在实践中,您可能必须查看使用此模板的现有页面,了解它们的功能,并承诺仅支持您的端口中的那些页面。

(您可能还想查看该工作流代码的用途。也许在您的实时目标上配置最低批准状态将使您能够删除此代码。)

我完全赞同努诺最初的建议,借机重新写一些东西。即使您有时间限制,除非您了解问题并实现代码,否则您永远无法成功地获得正确的逻辑。说真的 - 像这样无法维护的代码只会在您遇到严格的截止日期时造成更大的伤害。

处理不同类型的组件表示是 Tridion 页面模板中的常见要求。复合模板中的模板习语非常不同,但如果做得正确,它比这里显示的方法更容易理解。有一个base class available at the Tridion practice project,当您有机会以惯用方式编写代码而不是逐行移植时,您可能会发现它很有帮助。

【讨论】:

    猜你喜欢
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 2017-11-13
    • 2018-02-21
    • 2015-08-22
    • 2016-01-16
    • 2020-10-23
    相关资源
    最近更新 更多