x First time here? Check out the FAQ
Join us a the Umbraco event of the year - get your Codegarden 14 early bird ticket today
  • Avatar73posts121karma

    Getting page fields recursively in razor macro

    Alex Burr started this topic March 1, 2012 @ 09:03

    Hi everyone,

    I have a feature in my Umbraco site where sidebar content can be defined once, and it will persist through all child pages until it is overridden. This works well enough by just using the following in my template:

    <umbraco:Item Field="sidebarContent" recursive="true" insertTextBefore="<aside class='sidebar'>" insertTextAfter="</aside>" runat="server"></umbraco:Item>

    However what I want to do now is allow a per-page "hide sidebar" flag that can remove the sidebar at any point in the tree. So I added a true/false page field alias "hideSidebar" and changed by template to use the following:

    <umbraco:Macro filelocation="~/macroScripts/sidebar.cshtml" runat="server"></umbraco:Macro>

    Then in my .cshtml file I have the following:

    @{
    if (!String.IsNullOrEmpty(Model.hideSidebar.ToString()))
        {
            if (!Model.hideSidebar)
            {
               <aside class="sidebar">
                   @Model._sidebarContent
               </aside>
            }
        }
    }
    

    Unfortunately it doesn't seem to be working. I've debugged the conditional logic so I know that isn't the problem, and the <aside> is printed, just with nothing in it. When I switch the @Model._sidebarContent to just @Model.sidebarContent, I can get the content for the current page as expected.

    So the problem is with the recursion. Clearly I'm doing something wrong syntactically.

    Basically what I want is this:

    <umbraco:Item field="sidebarContent" recursive="true"></umbraco:Item>

    in the middle of a macro.

    For reference, I was reading through this topic but I couldn't quite seem to get it working.


  • Avatar73posts121karma
    Comment with ID: 109671
    Alex Burr posted this reply March 1, 2012 @ 09:14

    Looks like I got it working:

    @{    
        if (!String.IsNullOrEmpty(Model.hideSidebar.ToString()))
        {
    
            if (!Model.hideSidebar)
            {
               <aside class="sidebar">
                   @Model.AncestorsOrSelf().Where("sidebarContent.ToString() != string.Empty").Last().sidebarContent
               </aside>
            }
        }
    }

    However I could still use some guidance/feedback on what I could do better.


  • Avatar73posts121karma
    Comment with ID: 109673
    Alex Burr posted this reply March 1, 2012 @ 10:15

    Well I was wrong. The @Model expression I used will render richtext content, but it will not render macros that have been inserted in the editor.

    What I really need is to duplicate the 

    <umbraco:Itemfield="sidebarContent"recursive="true"></umbraco:Item>

    functionality in my macro.

    Or better yet, is there way to include conditional logic in my template around the <umbraco:Item> call?


  • Avatar3036posts8600karma
    adminComment with ID: 109675
    Lee Kelleher posted this reply March 1, 2012 @ 11:33

    Hi Alex,

    I asked the same question... Recursive fields using Razor macro

    Take a read over the replies, there are quite a few ways to do it.

    Cheers, Lee.


  • Avatar3036posts8600karma
    adminComment with ID: 109676
    Lee Kelleher posted this reply March 1, 2012 @ 11:35

    Ah, sorry, just noticed that you'd already seen my post about it.

    Wouldn't my example work for you?

    <umbraco:Macro runat="server" language="cshtml" sidebarContent="[$sidebarContent]">
            <aside class="sidebar">@Parameter.sidebarContent</aside>
    </umbraco:Macro>

  • Avatar73posts121karma
    Comment with ID: 109747
    Alex Burr posted this reply March 2, 2012 @ 04:56

    Lee,

    I appreciate the reply, but when I put that code in my template, the result is showing markup in my rendered page instead of the final desired output.

    Also, I need this to stay recursive so that the content can be set at one level and persist to child nodes, but be overriden or hidden at each node if necessary (see above).


  • Avatar3036posts8600karma
    adminComment with ID: 109749
    Lee Kelleher posted this reply March 2, 2012 @ 05:01

    Quick check... which version of Umbraco are you using? I *think* the underscore prefix was only added in v4.7.1 (but not sure if it's v4.7.1.1 though?)


  • Avatar73posts121karma
    Comment with ID: 109750
    Alex Burr posted this reply March 2, 2012 @ 05:02

    Yeah, I'm in 4.7.0 (Assembly version: 1.0.4090.38017)


Please login or Sign up To post replies