wingyip
wingyip

Reputation: 3536

Umbraco 6.2.0 Render maco inside a razor macroscript

I have the below code which works but is there a better way using the RenderMacroContent method. Not sure how to add parameters to that.

<umbraco:Macro runat="server" language="cshtml">@{
HtmlTextWriter writer = new HtmlTextWriter(this.Output);
var macroPressLogin = new umbraco.presentation.templateControls.Macro();
macroPressLogin.Alias = "Security-PressLogin";
macroPressLogin.Attributes.Add("TargetNode", Parameter.TargetNode);
macroPressLogin.RenderControl(writer);   }</umbraco:Macro>

Upvotes: 0

Views: 1447

Answers (2)

Hideous1
Hideous1

Reputation: 126

I know this question is a year old, but I hope someone finds a use for this information (using Umbraco version 6.x):

@(Html.Raw(umbraco.library.RenderMacroContent("<?UMBRACO_MACRO macroAlias=\"MacroAliasHere\" dynamicParameter=\""+ @dynamicValue + "\" staticParameter=\"staticValue\" ></?UMBRACO_MACRO>", Model.Id)))

Option number 2, which I prefer, is actually code to output a contour form using a value from the form picker datatype:

var helper = new UmbracoHelper(UmbracoContext.Current);
@Html.Raw(helper.RenderMacro("umbracoContour.RazorRenderForm", new { formGuid = @Model.formPickerAlias }))

Upvotes: 0

Rob Purcell
Rob Purcell

Reputation: 1285

As long as your view inherits from Umbraco.Web.Mvc.UmbracoTemplatePage or Umbraco.Web.Mvc.UmbracoViewPage<YourModel> you should just be able to write something like:

@Umbraco.RenderMacro("MacroAlias", new { ParameterAlias1 = "value1", ParameterAlias2 = "value2" })

So in your case it would be:

@Umbraco.RenderMacro("Security-PressLogin", new { TargetNode = "targetNodeValue" })

If you're talking about calling one macro from within another then this should also work as long as your macro partial view inherits from Umbraco.Web.Macros.PartialViewMacroPage

From your example it looks like you're working with a legacy razor macro using umbraco.MacroEngines. If possible I would recommend upgrading to a partial view macro. Click here for some further info.

Upvotes: 1

Related Questions