Reputation: 757
I have a macro call in my masterpage that sets the body class depending on a number of factors (depth, URL path, etc) However, I've stripped it down to find out why the macro is adding an extra space in the final html.
masterpage macro line:
<body class="<umbraco:Macro Alias="Bodyclass" runat="server"></umbraco:Macro>">
Simplified macro:
@{
var bodyClass = "";
if ((Model.NodeTypeAlias == "WLHomePage" ) ||
(Model.NodeTypeAlias == "WLHomeSubPage" ))
{ bodyClass = "front"; } else {
bodyClass = "not-front"; }
@bodyClass;
}
Returns:
<body class="
front">
Note the extra space between the first doublequote of the class=" and the actual class string.
Upvotes: 0
Views: 1465
Reputation: 69
@{
if (Model.NodeTypeAlias == "WLHomePage" || Model.NodeTypeAlias == "WLHomeSubPage")
{
return "front";
}
else
{
return "not-front";
}
}
Try it where it just returns a value instead.
Upvotes: 1
Reputation: 757
My fellow .NET developer cracked this one: It's the white space in the first line. Move the var up, like so:
@{ var bodyClass = "";
and >poof< whitespace gone.
So is this an umbraco bug?
Thanks all!
Upvotes: 0
Reputation: 10942
Remove Extra Newlines
Ensure that you don't have an extra new line before or after your macro code. You may want to open the cshtml file in a text editor to check that. After doing some testing on my end (using v4.8) that's the only condition I can find in which the extra newline is added. Using razor comments (@* ... *@
) before the code will also add a newline.
Alternative
Another alternative for adding the class to the tag could be writing the tag out with razor, like so:
<umbraco:Macro language="cshtml" runat="server">
@{
string bodyClass = "not-front";
if ((Model.NodeTypeAlias == "WLHomePage" ) ||
(Model.NodeTypeAlias == "WLHomeSubPage" ))
{
bodyClass = "front";
}
@:<body class="@bodyClass">
}
</umbraco:Macro>
...
</body>
Upvotes: 0
Reputation: 663
Try using Html.Raw to output the html. So something like:
@{
var bodyClass = "";
if(...)
{
bodyClass = "front";
}
else
{
bodyClass = "not-front";
}
@(Html.Raw(@bodyClass));
}
Upvotes: 0