Skip to content

Instantly share code, notes, and snippets.

@rbaty-barr
Created September 4, 2019 13:29
Show Gist options
  • Save rbaty-barr/aaccb707852746a5abf1aa02a218e651 to your computer and use it in GitHub Desktop.
Save rbaty-barr/aaccb707852746a5abf1aa02a218e651 to your computer and use it in GitHub Desktop.
display custom list of insights, the most recent five OR filter by tag
@inherits UmbracoViewPage<Lecoati.LeBlender.Extension.Models.LeBlenderModel>
@*
topInsights
filterByTag
*@
@if (Model.Items.Any())
{
var isFrontEnd = Lecoati.LeBlender.Extension.Helper.IsFrontEnd();
// Create blank list to add to in foreach loop
var combinedList = new List<IPublishedContent>();
foreach (var item in Model.Items)
{
var insights = Umbraco.TypedContentAtRoot().First().Descendants("insight").Where(x => x.IsVisible()).OrderByDescending(x => x.GetPropertyValue<DateTime>("insightDate")).Take(5);
if(item.GetValue<IEnumerable<IPublishedContent>>("topInsights") != null){
insights = item.GetValue<IEnumerable<IPublishedContent>>("topInsights");
}
if(item.GetValue<IEnumerable<IPublishedContent>>("filterByTag") !=null){
var theTags = item.GetValue<IEnumerable<IPublishedContent>>("filterByTag").ToContentSet();
var theFilter = "";
var orText = "";
foreach(var tag in theTags){
var theIndex = Convert.ToInt32(tag.Index());
if(theTags.Count() > 1 && (theIndex +1) < theTags.Count()){
orText = " || ";
} else {
orText ="";
}
theFilter += "z.GetPropertyValue<IEnumerable<IPublishedContent>>(\"contentTags\").Select(y => y.Id).Contains(" + tag.Id + ")" + orText;
}
var theQuery = "Umbraco.TypedContentAtRoot().First().Descendants(\"insight\").Where(x => x.IsVisible()).Where(z =>" + theFilter + ")";
insights = Umbraco.TypedContentAtRoot().First().Descendants("insight").Where(x => x.IsVisible());
@* Umbraco.TypedContentAtRoot().First().Descendants("insight").Where(x => x.IsVisible()).OrderByDescending(x => x.GetPropertyValue<DateTime>("insightDate"))
.Take(5);*@
<p>@theQuery</p>
}
var topInsight = insights.Take(1);
var otherInsights = insights.Skip(1).Take(4);
var theCount = insights.Count();
if(isFrontEnd){
<div class="row">
@foreach(var insight in topInsight){
var theImage = insight.GetPropertyValue<IPublishedContent>("insightImage");
var bgImage = "";
var insightTitle = insight.HasValue("insightTitle") ? insight.GetPropertyValue("insightTitle") : insight.Name;
if(theImage != null){
bgImage= theImage.GetCropUrl("resourceImage");
}
@*<p>@insight.GetPropertyValue<IEnumerable<IPublishedContent>>("contentTags").ToString()</p>*@
<div class="col-sm-6">
<div class="embed-responsive embed-responsive-1by1 position-relative">
<div class="embed-responsive-item resource-block-image" style="background-image: url(@(bgImage))">
<div class="resource-block-caption"><a href="@insight.Url" class="stretched-link">@insightTitle</a></div>
</div>
</div>
</div>
}
<div class="col-sm-6">
@foreach(var insightGroup in otherInsights.InGroupsOf(2)){
<div class="row my-n3">
@foreach(var insight in insightGroup){
var theImage = insight.GetPropertyValue<IPublishedContent>("insightImage");
var bgImage = "";
var insightTitle = insight.HasValue("insightTitle") ? insight.GetPropertyValue("insightTitle") : insight.Name;
if(theImage != null){
bgImage= theImage.GetCropUrl("resourceImage");
}
<div class="col-sm-6 py-3 px-3 mb-3">
<div class="embed-responsive embed-responsive-1by1">
<div class="embed-responsive-item resource-block-image" style="background-image: url(@(bgImage))">
<div class="resource-block-caption"><a href="@insight.Url" class="stretched-link">@insightTitle</a></div>
</div>
</div>
</div>
}
</div>
}
</div>
</div>
} else {
<p>@(theCount)</p>
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment