I have some Razor scripts that need to be broken down into separate Razor script, but still called from one main script. I've looked at the RenderMacroContent library method, but not sure if it is the best way to go.
I have done this by using the umbraco.library.RenderMacroContent. The method parameters themselves are not too pretty, but it works just fine. Here is an example, that also shows how to pass parameters:
Lets say you have a Razor macro called "Main" and another called "Hello"
"Main.cshtml" Razor
<p>This is the main.cshtml file. We will call add the rendered Hello.cshtml to the output.</p>
Remember to add parameters to the macro in umbraco.
Add this method to a code library somewhere. The code is alittle messy, but it does the job:
public static string RenderMacro(string macroName, int pageId, params string[] parameters) { string pars = ""; if (parameters != null) { foreach (var set in parameters) { string[] parts = set.Split('='); if (parts.Length == 2) pars += string.Format(" {0}=\"{1}\"", parts[0].Trim(), parts[1].Trim()); } }
I know i'm a bit late with this one but i've just released the Razor Components package, my package adds support for rendering macros from other Razor scripts.
This is specificly for when you want to use real macros, nothing wrong with just using
Calling Razor from Razor
I have some Razor scripts that need to be broken down into separate Razor script, but still called from one main script. I've looked at the RenderMacroContent library method, but not sure if it is the best way to go.
have a try with @RenderPage. for example:
You can also share @helper functions by storing them in the App_Code folder, for example: Navigation.cshtml:
From your other razor file you can call this as Filename.Helpername(argument), in this case:
I have done this by using the umbraco.library.RenderMacroContent. The method parameters themselves are not too pretty, but it works just fine. Here is an example, that also shows how to pass parameters:
Lets say you have a Razor macro called "Main" and another called "Hello"
"Main.cshtml" Razor
"Hello.cshtml" Razor
Remember to add parameters to the macro in umbraco.Add this method to a code library somewhere. The code is alittle messy, but it does the job:
I know i'm a bit late with this one but i've just released the Razor Components package, my package adds support for rendering macros from other Razor scripts.
This is specificly for when you want to use real macros, nothing wrong with just using
is working on a reply...