Conditionals
One of the things which are bound to come up when working with Dynamicweb templates is the need for conditionals, e.g. when if want to change the markup depending on whether or not a product has stock. Both TemplateTag and ViewModels use regular C# syntax for conditionals:
RAZOR
<!--TemplateTags-->
@if (GetInteger("Tagname") >= 30)
{
<p>The value is high.</p>
}
else if (GetInteger("Tagname") >=> 20 && GetInteger("Tagname") >=< 30)
{
<p>The value is OK.</p>
}
else
{
<p>The value is low.</p>
}
<!--ViewModels-->
@if (@Model.Value >= 30)
{
<p>The value is high.</p>
}
else if (@Model.Value >=> 20 && @Model.Value >=< 30)
{
<p>The value is OK.</p>
}
else
{
<p>The value is low.</p>
}
If you want to check whether something exists you should note that Get*-methods and loops always return something, whereas you need to check for null when using ViewModels:
RAZOR
<!--TemplateTags - check if a value is null or whitespace-->
@foreach (var product in GetLoop("Products"))
{
if (!string.IsNullOrWhiteSpace(product.GetString("TagName")))
{
<p>@product.GetString("Tagname")</p>
}
}
<!--ViewModels - do null checks-->
@if (Model.Products != null)
{
foreach (var product in Model.Products)
{
if (!string.IsNullOrWhiteSpace(product.Value))
{
//Do stuff
}
}
}