Come work for Umbraco - The Umbraco HQ are hiring Project managers, .NET developers and DevOps people!
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:
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.
Looks like I got it working:
@Model.AncestorsOrSelf().Where("sidebarContent.ToString() != string.Empty").Last().sidebarContent
However I could still use some guidance/feedback on what I could do better.
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
functionality in my macro.
Or better yet, is there way to include conditional logic in my template around the <umbraco:Item> call?
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.
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]">
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).
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 v126.96.36.199 though?)
Yeah, I'm in 4.7.0 (Assembly version: 1.0.4090.38017)