Need help with transform

Eric Erskine asked on May 4, 2017 18:19

I have a txt/xml transform that I need to convert to a .aspx transform. I have this piece of code in the txt transform that I need to convert to be used in the .aspx one. Can someone help?

{% Documents[NodeAliasPath].Children.Where("ClassName='cms.menuitem'").Count > 0 ? "dropdown" : "" #%}

Thanks, Eric from Moonstone Interactive

Correct Answer

Brenden Kehren answered on May 4, 2017 19:24

This is very similar to the solutions already provided but it's what I use. Keep in mind none of these have very good error handling and could cause problems.

<script runat="server"> 
    bool hasChildren;
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        hasChildren = ValidationHelper.GetBoolean(CMS.DocumentEngine.DocumentHelper.GetDocuments("CMS.MenuItem").NestingLevel(1).Path(Eval<string>("NodeAliasPath"), CMS.DocumentEngine.PathTypeEnum.Children).Where("DocumentMenuItemHideInNavigation = 0").Count > 0, false);
    }
</script>

Then use it inline like so:

class="<%# If(hasChildren), "dropdown", "") %>"

0 votesVote for this answer Unmark Correct answer

Recent Answers


Rui Wang answered on May 4, 2017 18:29

That's a special macro method. Is there a reason you need to convert it to ASCX transformation?

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on May 4, 2017 18:52

Should be something like:

<script runat="server">
      string MyText = "";

      protected override void OnInit(EventArgs e)
      {
               MyText = (CMS.DocumentEngine.DocumentHelper.GetDocuments("cms.menuitem").Path(NodeAliasPath, PathTypeEnum.Children).CombineWithDefaultCulture(true).Count > 0)? "dropdown" : "";
      }
    </script>

    <!--  your other transformation code -->
<%#MyText %>
1 votesVote for this answer Mark as a Correct answer

Rui Wang answered on May 4, 2017 19:00

Peter, I think the way you have it would not get the right result, because the NodeAliasPath would always be the CurrentDocument, not necessary the sub level node. (e.g. the transformation is used in a repeater one L0, and you are counting L2 items for L1 node, that NodeAliasPath will always to L0, not L1.).

At least I had that issue last time and end up doing something like this <%# CMS.DocumentEngine.DocumentHelper.GetDocuments().NestingLevel(1).Path(Eval("NodeAliasPath").ToString()+ "/%").Count() %>

0 votesVote for this answer Mark as a Correct answer

Eric Erskine answered on May 4, 2017 19:23 (last edited on May 4, 2017 19:25)

I have a menu that should only display fly-down if there are children items. However, I only want children items that are marked as "Show in navigation" on the navigation tab. I am using a hierarchical viewer to create the menu which uses a content filter of type "CMS.MenuItem" This allows me to have have different HTML output for each layer of menus.

The issue I have faced in the past is that most methods return child items no matter what type they are. If you guys have a better way to accomplish this, I'm open to changing my method.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.