Copied to clipboard

Flag this post as spam?

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


  • Tom 713 posts 954 karma points
    Jun 29, 2011 @ 09:41
    Tom
    0

    How To Escape Chars?

    Hi Guys,

    very new to razor and giving it a go converting some of my xslt macros to razor for practice.. just wondering what im doing wrong here. I can't seem to get the pipe appearing as plain text.. it's throwing an error:

    <umbraco:Macro runat="server" language="cshtml">
      @inherits umbraco.MacroEngines.DynamicNodeContext

      @if (@Model.pageTitle.EndsWith("| Site Name"))
      {
        @Model.pageTitle
      }
      @else
      {
        @Model.pageTitle | Site Name;
      }
    </umbraco:Macro>
  • Tom 713 posts 954 karma points
    Jun 29, 2011 @ 09:45
    Tom
    0

    and I've also tried:

    <umbraco:Macro runat="server" language="cshtml">
      @inherits umbraco.MacroEngines.DynamicNodeContext

      @if (@Model.pageTitle.EndsWith("| Site Name"))
      {
        @Model.pageTitle
      }
      else
      {
        @Model.pageTitle <text>{| 
    Site Name }text>
      }
    umbraco:Macro>
  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Jun 29, 2011 @ 09:48
    Sebastiaan Janssen
    0

    A few too many @ signs there. It takes getting used to.. :)

    Also, you need to wrap the output in some kind of HTML tag, so the page title is wrapped in a span here, that should work:

    <umbraco:Macro runat="server" language="cshtml">
      @inherits umbraco.MacroEngines.DynamicNodeContext
     
      @if (Model.pageTitle.EndsWith("| Site Name"))
      {
        <span>@Model.pageTitle</span>
      }
      else
      {
        <span>@Model.pageTitle | Site Name</span>
      }
    </umbraco:Macro>
  • Alex 78 posts 136 karma points
    Jun 29, 2011 @ 09:53
    Alex
    0

    Hi Tom,

    Try putting a span around what should be plain text html so like this

    <span>@Model.pageTitle | Site Name</span>

    You can think of Razor  switching between code contexts (i.e. c# and html) like a last in first out stack. Incidentally you don't need the @ sign before the Model in the if statement (because you are already in c# mode).

  • Tom 713 posts 954 karma points
    Jun 29, 2011 @ 09:53
    Tom
    0

    Hi Sebastiaan! thanks so much for such a speedy reply! so I can't wrap the macro tags themselves in a <title> tag? i.e.

     

    <title>
    <umbraco:Macrorunat="server"language="cshtml">
      @inherits umbraco.MacroEngines.DynamicNodeContext
     
      @if (Model.pageTitle.EndsWith("| Site Name"))
      {
       
    <span>@Model.pageTitle</span>
      }
      else
      {
       
    <span>@Model.pageTitle | Site Name</span>
      }
    </umbraco:Macro>
    </title>
  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Jun 29, 2011 @ 09:57
    Sebastiaan Janssen
    0

    I think that because you're switching to a new context (a Razor macro), the Razor engine doesn't know that it's got a tag wrapped around it. 

    Instead of <span>, try <text> though, that won't render anything but the title and should not trip up the render engine.

  • Alex 78 posts 136 karma points
    Jun 29, 2011 @ 10:02
    Alex
    1

    You could also use the @: syntax to output without any HTML rendering, so:

    @:@Model.pageTitle | Site Name

     Also could you not just bring the <title> tags inside the macro and if statement?

    <title>@Model.pageTitle | Site Name</title>
  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Jun 29, 2011 @ 10:17
    Sebastiaan Janssen
    0

    Alex: Great minds... ;)

    One more thing, you should be able to wrap the @if in the title tag so you don't repeat yourself:

    <title>
      @if(..) { }
      else { } 
    </title> 
  • Alex 78 posts 136 karma points
    Jun 29, 2011 @ 10:22
    Alex
    0

    Seb: Yes, I should really refresh before I post after having the page open for a while!

    If you put the title tags outside of the if then you will still need to use <text> or @: because the if statement will put you back into "code mode" (as I like to refer to it!).

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Jun 29, 2011 @ 10:31
    Sebastiaan Janssen
    0

    I hadn't tested it, but yes, you are correct, scrap that then. :-)

  • Tom 713 posts 954 karma points
    Jun 30, 2011 @ 03:57
    Tom
    0

    ohh what is the @:@ syntax?

  • Tom 713 posts 954 karma points
    Jun 30, 2011 @ 03:59
    Tom
    0
    <umbraco:Macro runat="server" language="cshtml">
      @inherits umbraco.MacroEngines.DynamicNodeContext
     
      @if(Model.pageTitle.EndsWith("| Site Name"))
      {
          <title>@Model.pageTitle</title>
      }
      else
      {
          <title>@Model.pageTitle | Site Name</title>
      }
    </umbraco:Macro>

    getting Error loading Razor Script 

    Cannot invoke a non-delegate type

  • Tom 713 posts 954 karma points
    Jun 30, 2011 @ 05:50
    Tom
    0
    <umbraco:Macro runat="server" language="cshtml">
      @inherits umbraco.MacroEngines.DynamicNodeContext
     
      @if(!string.IsNullOrEmpty(Model.pageTitle))
      {
        <title>@Model.pageTitle</title>
      }
     
    </umbraco:Macro>

    not even that works.. says the arg is incorrect.. not a string

  • Tom 713 posts 954 karma points
    Jun 30, 2011 @ 06:02
    Tom
    0

    Just wondering.. I had the error in the other post to do with writing out an property from a razor macro.. what is the best way to check for null etc? i.e. what datatype is Model.someproperty by default? i ended up doing:

      @if((Model.pageTitle != null && !string.IsNullOrEmpty(Model.pageTitle.ToString())) || (Model.siteName != null && !string.IsNullOrEmpty(Model.siteName.ToString())))
      {
        <title>@Model.siteName</title>
      }

    but surely there's a cleaner way!?

  • Alex 78 posts 136 karma points
    Jun 30, 2011 @ 09:44
    Alex
    0

    Hit Tom,

    The @: synatx basically forces the razor parser back into Markup mode for the following line, it's basically the same as doing <text>some plain text</text>.

    The Razor implementation within Umbraco tries to duck-type the property into a certain type based on some certain factors, for example if you have a datatype of textstring called myNumber on your docType with the value of "123" then Model.myNumber will be duck-typed to an int, but if you change it have value of "abc" then Model.myNumber will return a string type.

    Hope that makes sense?

    Alex

  • Biagio Paruolo 1594 posts 1825 karma points c-trib
    Oct 10, 2014 @ 21:54
    Biagio Paruolo
    0

    you can use @:

Please Sign in or register to post replies

Write your reply to:

Draft