Copied to clipboard

Flag this post as spam?

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


  • Janet Kirklen 102 posts 212 karma points
    Jul 02, 2012 @ 16:37
    Janet Kirklen
    0

    Syntax for Inline Razor Macros

    I'm running 4.7.2 and I'm having some parse issues with some of my inline Razor code. I can't seem to find a definitive answer about proper syntax.

    Which of the following is correct?

    <div class="bannermsg">

    <umbraco:Macro runat="server" language="cshtml">                                                        
            <href="@Model.NodeById(Model.banner1Link).Url">@Html.Raw(@Model.banner1Message)</a>                          
    </umbraco:Macro>

    </div>

    Or this?

     

    <div class="bannermsg">

    <umbraco:Macro runat="server" language="cshtml"

     

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{                                                      

             <href="@Model.NodeById(Model.banner1Link).Url">@Html.Raw(@Model.banner1Message)</a>                          

              }
    </umbraco:Macro>

    </div>

     

    Is there a good reference that cleary states what the best practice is for inline Razor macros?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jul 02, 2012 @ 18:15
    Jeroen Breuer
    0

    For inline Razor I think that less is better. If you need all kind of references and things get a bit more complicated than it's best to use a separate Razor Macro.

    Jeroen

  • Funka! 398 posts 661 karma points
    Jul 02, 2012 @ 21:22
    Funka!
    0

    I too would trend towards your first example if possible, although you might be having trouble with one too many "@" symbols in there...Once you call @Html.Raw, anything you pass further into that no longer needs any other @ symbols, because your context has already switched to "code-context" instead of markup-context.

    I also wonder if the "NodeById" method on your Model obj is problematic. I myself use @Library.NodeById usually for this, but if this works on Model as well then I just haven't seen that before.

    Try changing your first example to this instead?

    <div class="bannermsg">
      <
    umbraco:Macro runat="server" language="cshtml">                                                        
        <href="@Library.NodeById(Model.banner1Link).Url">@Html.Raw(Model.banner1Message)</a>                          
    </umbraco:Macro>
    </
    div>

    Good luck!

  • Janet Kirklen 102 posts 212 karma points
    Jul 02, 2012 @ 23:43
    Janet Kirklen
    0

    Thank you for the speedy replies.  My question was motivated by an issue i've been having with an itermitent Error Loading MacroEngine script (file: ) error.  On the page the first 2 inline scripts run fine and 3 one breaks and all subsequent ones (2-3 more) on the page break all with the same error.  Confusing because most of the time the page loads fine.  I suspected is was a parsing issue or a syntax problem.  I'm thinking now that the extra @ might be the culprit that throws off the parser. Since I've done that 3 or more times on the page. 

    I'm assuming that the @Model.NodeById is ok based on this resource page:

    http://our.umbraco.org/wiki/reference/code-snippets/razor-snippets/dynamicnode-(and-model)-members-and-properties

    But would welcome clarification on that.  With the various libraries and kinds of code mixed on the page it can get a bit confusing.  

    I've removed the extra @s.  We'll see how the page behaves and if that's it i'll mark this ias solved. 

Please Sign in or register to post replies

Write your reply to:

Draft