Umbraco Razor Crops - One way of checking if the Crop exists
This bit of code (highlighted yellow) writes out the Count of a cropped image as a comment, if it fails a default image is written out.
@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.cms.businesslogic.template;
@using System.Xml.XPath
@{
int foundMatch = 0;
string outPutText = "";
foreach (var page in Model.Children)
{
var thisImage = page.homePageImage;
var mediaItem = Model.MediaById(@thisImage);
if (@mediaItem.CroppedImages.Count() > 0)
{
<hr />
@Html.Raw("<article class=\"\">");
<div>
<div class="one_half first">
<a href="@page.Url" title="@page.navigationName">
@try
{
@Html.Raw("<!--" + @mediaItem.CroppedImages.Find("@name", "mediumImage").Count() + "-->");
<img alt="@page.Name" src="@mediaItem.CroppedImages.Find("@name", "mediumImage").url" />
}
catch
{ <img alt="@page.Name" src="/media/117165/default-video-image.jpg" />
}
</a></div>
<div class="one_half">
@if (@page.navigationName != "")
{
<h2>@page.navigationName</h2>
}
<p>
@if (@page.teaserText.Length >= 130)
{
@page.teaserText.Substring(0,130) @Html.Raw("...");
}
else
{
@page.teaserText;
}
</p>
<footer class="read-more"><a href="@page.Url" title="@page.pageHeading">Read More »</a></footer>
</div>
</div>
@Html.Raw("</article><div class=\"clear\"> </div>");
<hr />
}
}
}
@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.cms.businesslogic.template;
@using System.Xml.XPath
@{
int foundMatch = 0;
string outPutText = "";
foreach (var page in Model.Children)
{
var thisImage = page.homePageImage;
var mediaItem = Model.MediaById(@thisImage);
if (@mediaItem.CroppedImages.Count() > 0)
{
<hr />
@Html.Raw("<article class=\"\">");
<div>
<div class="one_half first">
<a href="@page.Url" title="@page.navigationName">
@try
{
@Html.Raw("<!--" + @mediaItem.CroppedImages.Find("@name", "mediumImage").Count() + "-->");
<img alt="@page.Name" src="@mediaItem.CroppedImages.Find("@name", "mediumImage").url" />
}
catch
{ <img alt="@page.Name" src="/media/117165/default-video-image.jpg" />
}
</a></div>
<div class="one_half">
@if (@page.navigationName != "")
{
<h2>@page.navigationName</h2>
}
<p>
@if (@page.teaserText.Length >= 130)
{
@page.teaserText.Substring(0,130) @Html.Raw("...");
}
else
{
@page.teaserText;
}
</p>
<footer class="read-more"><a href="@page.Url" title="@page.pageHeading">Read More »</a></footer>
</div>
</div>
@Html.Raw("</article><div class=\"clear\"> </div>");
<hr />
}
}
}
Comments
Post a Comment