Wednesday 12 February 2014

Umbraco render macro within macro

I wrote this helper method to easily render macros within other macros when using razor (cshtml) views. This also allows the passing in of parameters. This is an alternative to RenderMacroContent which didn't seem to work for me.

public static string RenderMacro(string alias, int nodeId, Dictionary<string string=> parameters)
        {
            var macroEngine = new RazorMacroEngine();
            var macro = new MacroModel();
            macro.ScriptLanguage = "cshtml";
            macro.ScriptName = alias + ".cshtml";

            foreach (var parameter in parameters)
            {
                macro.Properties.Add(new MacroPropertyModel(parameter.Key, parameter.Value));
            }

            return macroEngine.Execute(macro, new umbraco.NodeFactory.Node(nodeId));
        }


This is the code that wasn't working:

umbraco.library.RenderMacroContent(string.Format("</?UMBRACO_MACRO>", alias, args), model.Id)

No comments: