Copied to clipboard

Flag this post as spam?

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


  • andrew 13 posts 36 karma points
    Feb 22, 2012 @ 23:58
    andrew
    0

    Best way to call a macro from a macro

    I've seen a number of Inception-style macro requests, but most are old and involve XSLT which I've moved entirely away from.  Here's what I came up with, and I wanted to see if anyone knows a better way of doing things:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
        var personType = @Parameter.personType;
        var allPersonNodes = Model.AncestorOrSelf().DescendantsOrSelf("Person");
        HtmlTextWriter writer = new HtmlTextWriter(this.Output);
        foreach (var Person in allPersonNodes)
        {
            if (Person.personType == personType)
            {
                var personInfo = new umbraco.presentation.templateControls.Macro();
                personInfo.Alias = "PersonInfoSmall";
                personInfo.MacroAttributes.Add("personid", Person.Id);
                personInfo.RenderControl(writer);
            }
        }
    }

    My thanks go to MunkiMagik for the post here that pointed me in the right direction.   Since it's not an ASCX and I can't create a control holder to inject the macro into, I created an HtmlTextWriter that uses the page's TextWriter.

    My question is: is this the best way of doing this, or is there an easier / more acceptable way?

  • Douglas Ludlow 210 posts 366 karma points
    Feb 23, 2012 @ 00:08
    Douglas Ludlow
    0

    If it's another razor macro, you can use the RenderPage method. See the wiki for details.

  • andrew 13 posts 36 karma points
    Feb 23, 2012 @ 16:36
    andrew
    0

    Thanks Douglas!  That makes the code a little cleaner.

    Since I'm calling a macro that I also allow our content editors to drop into a page through the RTE, I've changed the parameter inputs to allow either method of calling:

    var Node = (@Parameter.personId == null) ? @Model.NodeById(PageData[0]) : @Model.NodeById(@Parameter.personId);
    

    Unless RenderPage is able to pass through named parameters?

  • Douglas Ludlow 210 posts 366 karma points
    Feb 23, 2012 @ 16:43
    Douglas Ludlow
    0

    You can also use Page properties to pass variables from one macro/helper to another. For example:

    @{
    Page.HelloWorld = Model.helloWorld;
    }

    @RenderPage("~/macroScripts/showHelloWorld.cshtml")

    showHelloWorld.cshtml:

    @{
    var helloWorld = Page.HelloWorld;
    }
    @helloWorld

     

  • andrew 13 posts 36 karma points
    Feb 23, 2012 @ 16:47
    andrew
    0

    The only problem there is that I'm using the outer macro to iterate through a list of people, so setting a page variable might not be the best bet.  I don't want to have to write my presentation logic twice, for things like people's contact info or news items.  I've got the basic macros (ShowPersonInfo, ShowNewsItem) that I can either call directly (if I want to add someone's contact info to a certain page, or display a certain news item) or call from another macro (like ShowNewsItemsByTag or ShowPersonInfoByPersonType).

Please Sign in or register to post replies

Write your reply to:

Draft