Copied to clipboard

Flag this post as spam?

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


  • Matt Taylor 859 posts 2052 karma points
    Apr 10, 2012 @ 17:09
    Matt Taylor
    0

    Dynamic macro parameter - passing the results of one macro to another?

    I'm using a macro that takes a list of node IDs as a parameter:

    <umbraco:Macro Alias="RenderTemplate" NodeIds="1201,1202,1203" runat="server"></umbraco:Macro>

    I was using a multi-node picker and passing in the values like so:

    <umbraco:Macro Alias="RenderTemplate" NodeIds="[$MainPages]" runat="server"></umbraco:Macro>

    I have however decided that it will be better if the editor does not have to maintain the multi-node list because it's really just a list of the top level pages.

    I would like to dynamically create the node ID list and then pass it to the RenderTemplate macro.

    I have this Razor script which will generate the node ID list:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{ 
        string ids = "";
        var parent = @Model.AncestorOrSelf(2);
        if (parent != null)
        {
            foreach (var page in parent.MainPage.Where("Visible"))
            {
                ids = @Library.Join(",", ids, page.Id);
         
            } 
        }
    }

    but is there someway to then use it in the RenderTemplate macro or is the only way to create a wrapper macro?

    Regards,

    Matt

  • Lee Kelleher 3945 posts 15163 karma points MVP 10x admin c-trib
    Apr 10, 2012 @ 18:42
    Lee Kelleher
    0

    Hi Matt,

    I'd go with the solution in the blog post... a wrapper macro - seems like the path of least resistance!

    I take it that you are using uComponents' RenderTemplate control? If so, you could probably just call that directly, no need for the extra macro container.

    I can provide a code snippet later if you want? (haven't got time at this moment) :-)

    Cheers, Lee.

  • Matt Taylor 859 posts 2052 karma points
    Apr 10, 2012 @ 18:48
    Matt Taylor
    0

    Hi Lee,

    Yes I'm using the uComponents RenderTemplate control.

    I have successfully implemented the solution in the blog post and it's working well.

    Always interested in seeing your method if you get the time.

    Regards,

    Matt

  • Matt Taylor 859 posts 2052 karma points
    Apr 10, 2012 @ 18:57
    Matt Taylor
    1

    I suspect you mean I could place the following in my control

    <%@ Register Assembly="uComponents.Core" Namespace="uComponents.Core.Controls" TagPrefix="uComponents" %>
    <uComponents:RenderTemplate id="myRenderTemplate" runat="server" />

    and then set the node ids in my code behind logic

    myRenderTemplate.NodeIds="1062,1066";

    I think that's better.

    :)

     

  • Lee Kelleher 3945 posts 15163 karma points MVP 10x admin c-trib
    Apr 10, 2012 @ 19:05
    Lee Kelleher
    0

    Hi Matt,

    Cool, I think that works very well! :-D

    The approach I was thinking would create the control in code...

    <%@ Import Namespace="uComponents.Core" %>
    <%@ Import Namespace="uComponents.Core.uQueryExtensions" %>
    
    <script runat="server">
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
    
            var level2Nodes = uQuery.GetNodesByXPath("//*[@isDoc and @level = 2]");
            if (level2Nodes != null)
            {
                var nodeIds = string.Join(",", level2Nodes.Select(n => n.Id));
                var renderTemplate = new uComponents.Core.Controls.RenderTemplate() { NodeIds = nodeIds };
                this.Controls.Add(renderTemplate); // change this to add the control to something better, like a PlaceHolder.
            }
        }
    </script>

    Since you are already using uComponents, you can take advantage of the uQuery helper methods to get the nodes that you want.

    As with most things in Umbraco, there are many ways to tackle these problems... all good fun! :-)

    Cheers, Lee.

  • Matt Taylor 859 posts 2052 karma points
    Apr 11, 2012 @ 14:16
    Matt Taylor
    0

    Thanks Lee,

    I certainly am using uQuery too! :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies