Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Edward Cooke 2 posts 32 karma points
    Jun 21, 2013 @ 00:22
    Edward Cooke
    0

    @helper methods not executing in 6.0.6

    Hi all, I have a weird rendering problem with Razor script files. Effectively, it doesn't execute anything inside of he helper methds. I even tried putting a int x=0;int y=0;int z=x/y; to get an exception, and it wasn't thrown.

    Another thing I have that does not work is @Html.Raw("") will not write anything out. What I have seen does work, is when you put a <ul></ul> in the body of script (not in the helpers) it would get written out. During a debugging step through of the code, it says it hits the helper, but never does anything in it, just jumps right back out, never goes through any of the lines in it.

    Am I doing this in the wrong spot in Umbraco?

    My script/macro:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines;
    
    @helper LI(DynamicNode item){
        <li>
            <a href="@(item.Url)">@(item.Name)</a>
            @{
                IEnumerable<DynamicNode> children = item.Children.Where(p => p.Visible);
                UL(children);
            }
        </li>
    }
    
    @helper UL(IEnumerable<DynamicNode> items){
        <ul>
            @{
                //divide by zero exception stuff was here
                foreach (DynamicNode item in items)
                {
                    LI(item);
                }
            }
        </ul> 
    }
    @{ 
        DynamicNode root = Model as DynamicNode;
        DynamicNode model = root;
        root = root.AncestorOrSelf(1);
    
        while (root.Parent != null)
        {
            root = root.Parent;
        }
    
        IEnumerable<DynamicNode> rootMenu = root.Children.Where(p => p.Visible);
        UL(rootMenu);
    
  • Theresa Danowski 16 posts 78 karma points
    Jun 21, 2013 @ 14:43
    Theresa Danowski
    100

    Hello Edward,

    you should call your helpers with @helpername. I tried your code this way and it was working.

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines;

    @helper LI(DynamicNode item){
    <li>
    <a href="@(item.Url)">@(item.Name)</a>
    @{
    IEnumerable<DynamicNode> children = item.Children.Where(p => p.Visible);
    @UL(children);
    }
    </li>
    }

    @helper UL(IEnumerable<DynamicNode> items){
    <ul>
    @{
    //divide by zero exception stuff was here
    foreach (DynamicNode item in items)
    {
    @LI(item);
    }
    }
    </ul>
    }
    @{
    DynamicNode root = Model as DynamicNode;
    DynamicNode model = root;
    root = root.AncestorOrSelf(1);

    while (root.Parent != null)
    {
    root = root.Parent;
    }

    IEnumerable<DynamicNode> rootMenu = root.Children.Where(p => p.Visible);
    @UL(rootMenu);
    }


    Hope it's working for you as well.

    Greets,

    Theresa

  • Edward Cooke 2 posts 32 karma points
    Jun 21, 2013 @ 23:30
    Edward Cooke
    0

    Thanks for the tip, it worked great. I've used razor in other MVC apps for the past year, but I've never used helper methods (didn't know they even existed), they are pretty neat. Its amazing how much you learn every day.

Please Sign in or register to post replies

Write your reply to:

Draft