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 = newHtmlTextWriter(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?
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?
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).
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:
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?
If it's another razor macro, you can use the RenderPage method. See the wiki for details.
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:
Unless RenderPage is able to pass through named parameters?
You can also use Page properties to pass variables from one macro/helper to another. For example:
showHelloWorld.cshtml:
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).
is working on a reply...