Copied to clipboard

Flag this post as spam?

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


  • Steve Kallal 9 posts 29 karma points
    Dec 21, 2011 @ 02:18
    Steve Kallal
    0

    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.

  • Owen 123 posts 246 karma points
    Dec 21, 2011 @ 06:37
    Owen
    2

    have a try with @RenderPage. for example:

    @RenderPage("Header.cshtml")
  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Dec 21, 2011 @ 09:09
    Sebastiaan Janssen
    0

    You can also share @helper functions by storing them in the App_Code folder, for example: Navigation.cshtml:

    @helper RenderMainMenu(dynamic model) {
      var parent = model.AncestorOrSelf(1);

      <ul>
      @foreach (var item in parent.Children.Where("Visible")) {
        <li><a href="@item.Url">@item.Name</a></li>
      }
      <ul>

    From your other razor file you can call this as Filename.Helpername(argument), in this case:

    @Navigation.RenderMainMenu(Model)
  • Allan Koch 9 posts 23 karma points
    Jan 18, 2012 @ 13:03
    Allan Koch
    0

    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>

    @Code.Razor.RenderMacro("Hello", Model.Id)
    <br />
    @Code.Razor.RenderMacro("Hello", Model.Id, "items=2", "page=3") 

    "Hello.cshtml" Razor

    <p>Hello from the Hello.cshtml file</p>

    @Parameter.page <br /> @Parameter.items 
    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());
        }
      }
      
      return umbraco.library.RenderMacroContent(
      string.Format("<?UMBRACO_MACRO macroAlias=\"{0}\" {1}></?UMBRACO_MACRO>", macroName, pars),
        pageId);

     

  • Scott Williams 14 posts 71 karma points
    Jun 20, 2012 @ 09:25
    Scott Williams
    0

    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

    @RenderPage("ScriptFileName.chstml")
Please Sign in or register to post replies

Write your reply to:

Draft