4

I have created a template using Razor Generator . now I need in a recursive function to create a nested list of items . I tried this solution but then all my codes became marked as errors .

@* Generator: Template *@


@functions
{
    public IList<Models.Category> Topics
    {
        get;
        set;
    }

}

@helper ShowTree(IList<Models.Category> topics)
{
    <ul>
        @foreach (var topic in topics)
        {
            <li>
                @topic.Title
                @if (topic.Childs.Count > 0)
                {
                    @{
                         ShowTree(topic.Childs);
                     }
                }
            </li>
        }
    </ul>
}

some of irrelevant errors I got after adding the helper :

-Error  3 Invalid expression term ';'
Error   4 Feature 'lambda expression' cannot be used because it is not part of the ISO-2 C# language specification
Error   13 Feature 'implicitly typed local variable' cannot be used because it is not part of the System C# language specification
Error   6 The name 'WriteLiteralTo' does not exist in the current context

but as I remove the helper method , all these just disappear !

did I do something wrong or creating helpers in Razor Templates is not possible ?

Community
  • 1
  • 1
mohsen dorparasti
  • 8,107
  • 7
  • 41
  • 61

2 Answers2

0

The code below will work.

@helper ShowTree(IList<Models.Category> topics)
{
    if (topics != null && topics.Any()) {
    <ul>
        @foreach (var topic in topics)
        {
            <li>
                @topic.Title
                @ShowTree(topic.Childs)
            </li>
        }
    </ul>
    }
}
jessegavin
  • 74,067
  • 28
  • 136
  • 164
  • 2
    RazorGenerator templates behave differently from how MVC works, as I've learned to my annoyance. So just because it works in an app doesn't mean it'd work here. – Bobson Nov 02 '12 at 14:35
  • That said, if it's just a code syntax issue, this may be the right fix anyway. – Bobson Nov 02 '12 at 14:36
  • @Bobson can you explain why the code I provided above does NOT work in RazorGenerator? Did you try it? Or did you just down vote it? – jessegavin Nov 02 '12 at 14:41
  • 1
    Valid point. Just because it works in a MVC app doesn't mean it *doesn't* work in a Template. Vote removed. – Bobson Nov 02 '12 at 14:45
  • @jessegavin : thank you , but no , it dosen't . as Bobson has said it seems that RazorGenerator templates behave differently from how MVC works – mohsen dorparasti Nov 02 '12 at 14:56
  • 2
    Well I'll be a monkey's uncle. – jessegavin Nov 02 '12 at 14:57
  • 1
    The problem I ran into is that a RazorGenerator template neither inherits from the standard template, nor from the standard view. So you can't `@Include`, and you don't have `@Html`. – Bobson Nov 02 '12 at 15:03
0

For what it's worth, since no one's provided a better answer, we gave up on using RazorGenerator templates for this exact reason.

My question is over here, and our answer was to set up a local MVC website on an internal server, and use that to render our templates. It's the only suggestion I've seen yet which has worked the way I wanted things to.

Community
  • 1
  • 1
Bobson
  • 13,498
  • 5
  • 55
  • 80