I have been struggling with this for a few days now and nearly have it but just can't seem to get it right.
I have a number of testimonials that are catagorised by a drop down menu.
I wish to display the catagory testimonial on the catagory page. ie painting testimonials on the painting page. And when there are no catagory testimonials a random testimonial is displayed from any catagory.
To control the display of the catagories I have a variable
var testip = Model.Content.Site().FirstChild("Testimonial").Children().RandomOrder().Take(1).Where(x => x.HasValue("testimonialCatagory") && x.GetPropertyValue<string>("testimonialCatagory").Equals("Painting"));
This works fine
The variable that displays any testimonial is
var testiall = Model.Content.Site().FirstChild("Testimonial").Children().RandomOrder().Take(1);
This works fine if the catagory has more than one testimonial. But if the catagory only has one testimonial there is sometimes a blank where the testimonial should be and then it appears. This also happens if there are no testimonials.
There are 12 catagories hence the else if statements.
// Your chaining is incorrect:
// you are taking one - before you check the condition
// so if that first one isn't a "Painting" then you won't take anything so testip will be empty.
// so the "where" clause should go before the take.
// you filter then take.
// use firstordefault - dont need foreach with first or default
// rename variables to be more meaningful
// also why is there an empty anchor link?
// also dont use <b> use <strong>
// don't duplicate same html
// put the conditional around the image element instead.
// change the 2nd conditional "else if" - it's not required as always results in true.
// refactor and put code into a helper
@{
// get 1 random painting testimonial
var paintingTestimonial = Model.Content.Site().FirstChild("Testimonial").Children().Where(x => x.HasValue("testimonialCatagory") && x.GetPropertyValue<string>
("testimonialCatagory").Equals("Painting")).RandomOrder().FirstOrDefault();
// 1 random testimonial
var randomTestimonial = Model.Content.Site().FirstChild("Testimonial").Children().RandomOrder().FirstOrDefault();
}
@if (Model.Content.Id == 1074 && paintingTestimonial != null)
{
@renderTestimonial(paintingTestimonial)
}
else if (randomTestimonial!= null)
{
@renderTestimonial(randomTestimonial)
}
@helper renderTestimonial(IPublishedContent testimonial)
{
var companyLogo = Umbraco.TypedMedia(testimonial.GetPropertyValue<string>("testifierCompanyLogo"));
if (companyLogo != null)
{
<div class="well">
<div class="testimonial">
<p>@testimonial.GetPropertyValue("testimonialText")</p>
<b>@testimonial.GetPropertyValue("testifierName")</b> - <i> @testimonial.GetPropertyValue("testifierCompany")</i>
@if (companyLogo != null)
{
<img paintingTestimonial="@companyLogo.Url" />
}
<a href="@testimonial.Url"></a>
</div>
</div>
}
}
Lol, I completely rewrite the code for you, you modify it slightly outside the parameters of your original question, then mark your own code as correct. shakes head
Random display of testimonials
I have been struggling with this for a few days now and nearly have it but just can't seem to get it right. I have a number of testimonials that are catagorised by a drop down menu. I wish to display the catagory testimonial on the catagory page. ie painting testimonials on the painting page. And when there are no catagory testimonials a random testimonial is displayed from any catagory.
To control the display of the catagories I have a variable
This works fine
The variable that displays any testimonial is
This works fine
To display the elements I have
This works fine if the catagory has more than one testimonial. But if the catagory only has one testimonial there is sometimes a blank where the testimonial should be and then it appears. This also happens if there are no testimonials.
There are 12 catagories hence the else if statements.
Does anyone have any ideas?
Thanks Matt
Thanks for pointing me n the right direction
I am learning alot. Did not know about the @helper.
Got there in the end with just a few modifications
Lol, I completely rewrite the code for you, you modify it slightly outside the parameters of your original question, then mark your own code as correct. shakes head
is working on a reply...