Copied to clipboard

Flag this post as spam?

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


  • Alex Burr 77 posts 128 karma points
    Mar 01, 2012 @ 21:03
    Alex Burr
    0

    Getting page fields recursively in razor macro

    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.

  • Alex Burr 77 posts 128 karma points
    Mar 01, 2012 @ 21:14
    Alex Burr
    0

    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.

  • Alex Burr 77 posts 128 karma points
    Mar 01, 2012 @ 22:15
    Alex Burr
    0

    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?

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Mar 01, 2012 @ 23:33
    Lee Kelleher
    0

    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.

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Mar 01, 2012 @ 23:35
    Lee Kelleher
    0

    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>
  • Alex Burr 77 posts 128 karma points
    Mar 02, 2012 @ 16:56
    Alex Burr
    0

    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).

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Mar 02, 2012 @ 17:01
    Lee Kelleher
    0

    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?)

  • Alex Burr 77 posts 128 karma points
    Mar 02, 2012 @ 17:02
    Alex Burr
    0

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

Please Sign in or register to post replies

Write your reply to:

Draft