I'm finding Umbraco's syntax disgusting verbose. I've been trying to an if else statement in a macroscript for half an hour and absolutely loosing the will to live.
I've got a custom property against a document type called showSlideshow which can either be true or false.
@if (@Model.showSlideshow == "") { } else { }
OBVIOUSLY fails and errors. I have no idea why.
Please tell me how to do an if else statement in a .cshtml file. I just want to be able to copy and paste it in so it will work.
I basically want to show the following if it's true:
Quite frankly I'm embarresed to be asking how to do such a remedial tasks, but it just goes to show my desecion to leave all Microsoft technologies behind was well founded. This time I really do mean 'never again'.
It's because the value of "Model.showSlideshow" is strongly-typed ... since the property is a true/false checkbox, the value would be a boolean. So when the script tries to compare a boolean to a string, the Razor engine/compiler borks! :-(
You even don't need a redundant variable. Simply "@if(Model.showSlideshow) { ... " should work ok. Initial errors caused as I can guess by that the "showSlideshow" property returns a boolean runtime value. The compiler allows comparing a dynamic-typed expression to any both dynamic- and static- typed one but doesn't perform any type conversion to cast both hands to the same runtime type. So such an expression just fails in runtime as soon as being executed.
One small thing he overlooked was that if one item is returned the output says "1 items were..." I want to add an if else block to catch the count and output the correct English, along the lines of:
if (matchingNodes.Count < 1) { @* No results were found for the specified tags *@ <p>No tagged items were found that matched the @tagsText: @searchFor</p> return; }
@* Some results were found for the specified tags *@ <p><strong>@matchingNodes.Count</strong>
Since you're in code block where you want to write out the text, you have to tell Razor you want to output this text rather than interpreting it as code. Here's a few different appraoches:
if (matchingNodes.Count = 1) { <span>item was</span> }
Or,
if (matchingNodes.Count = 1) { @: item was }
Or,
if (matchingNodes.Count = 1) { <text>item was</text> }
Thanks Tom, I gathered it was something do do with context and letting Razor know what it is expected to do. Because I am already in a text block I had to add @ to the if, and had to double the = as otherwise Razor thought I was trying to assign a value, so I finally got:
Childsplay - if else?????
I'm finding Umbraco's syntax disgusting verbose. I've been trying to an if else statement in a macroscript for half an hour and absolutely loosing the will to live.
I've got a custom property against a document type called showSlideshow which can either be true or false.
OBVIOUSLY fails and errors. I have no idea why.
Please tell me how to do an if else statement in a .cshtml file. I just want to be able to copy and paste it in so it will work.
I basically want to show the following if it's true:
Or another div if it's false.
Quite frankly I'm embarresed to be asking how to do such a remedial tasks, but it just goes to show my desecion to leave all Microsoft technologies behind was well founded. This time I really do mean 'never again'.
Hi Dominic, I hear your frustration!
I haven't used Razor much (more in the XSLT camp), but still I want to help.
Think you can get rid of the @ symbol before the "@Model" - because you are already in an "if" condition.
Hope that helps?
Cheers, Lee
} else {
Produces:
"Error loading MacroEngine script (file: InnerLandingSlides.cshtml)"
@if ("this" == "this")
{
} else {
}
doesn't error
Hi Dominic - quick check, which version of Umbraco are you using? v4.7.1 or v4.7.1.1 (aw, so many point-1s) ;-)
4.7.1.1
@{var show = @Model.showSlideshow;}
@if (show == "True")
{
<p>1</p>
}
else
{
<p>2</p>
}
Another fail. WTF.
@{var show = @Model.showSlideshow;}
@if ("@show" == "True")
{
<p>1</p>
}
else
{
<p>2</p>
}
FAIL
@{
var show = @Model.showSlideshow;
if (@show == "True")
{
<p>1</p>
}
else
{
<p>2</p>
}
}
FAIL
@{
var show = @Model.showSlideshow;
if (@show)
{
<p>1</p>
}
else
{
<p>2</p>
}
}
PASS. Jesus H Christ.
Hi Dominic,
It's because the value of "Model.showSlideshow" is strongly-typed ... since the property is a true/false checkbox, the value would be a boolean. So when the script tries to compare a boolean to a string, the Razor engine/compiler borks! :-(
Things that would work are...
Frustrating, I know - you're not alone. :-)
Cheers, Lee.
Right ok... well now I know, 1 hour later! Thanks.
You even don't need a redundant variable. Simply "@if(Model.showSlideshow) { ... " should work ok. Initial errors caused as I can guess by that the "showSlideshow" property returns a boolean runtime value. The compiler allows comparing a dynamic-typed expression to any both dynamic- and static- typed one but doesn't perform any type conversion to cast both hands to the same runtime type. So such an expression just fails in runtime as soon as being executed.
Ughm. I've posted my message a bit late. :-)
Thank you so much....code save my lot of time..
Thanks Again
I too am singularly unimpressed with razor, the bastard child of c and xsl. I have a fairly simple piece of code from http://daniel.streefkerkonline.com/tagging-and-umbraco/
One small thing he overlooked was that if one item is returned the output says "1 items were..." I want to add an if else block to catch the count and output the correct English, along the lines of:
===
@inherits umbraco.MacroEngines.DynamicNodeContext
@using System.Text
@using umbraco.MacroEngines
@using umbraco.cms.businesslogic.Tags
@{
string searchFor = Request["tags"];
if(string.IsNullOrEmpty(searchFor))
{
@* No tags were specified *@
<p>Please specify a tag to search for</p>
return;
}
var matchingNodes = Tag.GetNodesWithTags(searchFor).ToList();
string tagsText = searchFor.Split(',').Count() > 1 ? "tags" : "tag";
if (matchingNodes.Count < 1)
{
@* No results were found for the specified tags *@
<p>No tagged items were found that matched the @tagsText: @searchFor</p>
return;
}
@* Some results were found for the specified tags *@
<p><strong>@matchingNodes.Count</strong>
@* output the correct text*@
if (matchingNodes.Count = 1)
{
"item was";
}
else
{
"items were";
}
found that matched the @tagsText: "@searchFor"</p>
<ul>
@foreach (var node in matchingNodes)
{
dynamic dn = new DynamicNode(node.Id);
<li><a href="@dn.Url">@dn.Name</a></li>
}
</ul>
}
===
Anyone?
Hi William,
Since you're in code block where you want to write out the text, you have to tell Razor you want to output this text rather than interpreting it as code. Here's a few different appraoches:
Or,
Or,
Check out this post for more info: Razor Tip #2: Output Text in Code Context
Hope this helps,
Tom
Thanks Tom, I gathered it was something do do with context and letting Razor know what it is expected to do. Because I am already in a text block I had to add @ to the if, and had to double the = as otherwise Razor thought I was trying to assign a value, so I finally got:
@if(matchingNodes.Count == 1)
{
<text>item was</text>
}
else
{
<text>items were</text>
}
Which does the job a treat :)
is working on a reply...