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:
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).
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!).
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:
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.
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>
and I've also tried:
}
umbraco:Macro>
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:
Hi Tom,
Try putting a span around what should be plain text html so like this
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).
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>
</title>
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.
You could also use the @: syntax to output without any HTML rendering, so:
Also could you not just bring the <title> tags inside the macro and if statement?
Alex: Great minds... ;)
One more thing, you should be able to wrap the @if in the title tag so you don't repeat yourself:
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!).
I hadn't tested it, but yes, you are correct, scrap that then. :-)
ohh what is the @:@ syntax?
<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>
Cannot invoke a non-delegate type
<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
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!?
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
you can use @:
is working on a reply...