Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mark Watson 82 posts 253 karma points
    27 days ago
    Mark Watson
    0

    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

    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

    To display the elements I have

        if(Model.Content.Id == 1074 && testip != null){
            {
            foreach(var item in testip){
                                    var typedMediaPickerSingle = Umbraco.TypedMedia(@Umbraco.Field(item,"testifierCompanyLogo").ToString());
                                    if (typedMediaPickerSingle != null )
                {
                        <div class="well">
                      <div class="testimonial">
                       <p>@item.GetPropertyValue("testimonialText")</p>
                        <b>@item.GetPropertyValue("testifierName")</b> - <i> @item.GetPropertyValue("testifierCompany")</i>
                         <img src="@typedMediaPickerSingle.Url" /><a href="@item.Url"></a>
                   </div>
                   </div>
                    }
                    else
                    {
                    <div class="well">
                      <div class="testimonial">
                     <p>@item.GetPropertyValue("testimonialText")</p>
                     <b>@item.GetPropertyValue("testifierName")</b> - <i> @item.GetPropertyValue("testifierCompany")</i>
                     </div>
                     </div>
            } } }}
    else if (Model.Content.Id != null)
       {
       foreach(var item in testiall){
                                    var typedMediaPickerSingle = Umbraco.TypedMedia(@Umbraco.Field(item,"testifierCompanyLogo").ToString());
                                    if (typedMediaPickerSingle != null )
                {
                        <div class="well">
                      <div class="testimonial">
                       <p>@item.GetPropertyValue("testimonialText")</p>
                        <b>@item.GetPropertyValue("testifierName")</b> - <i> @item.GetPropertyValue("testifierCompany")</i>
                         <img src="@typedMediaPickerSingle.Url" /><a href="@item.Url"></a>
                   </div>
                   </div>
                    }
                    else
                    {
                    <div class="well">
                      <div class="testimonial">
                     <p>@item.GetPropertyValue("testimonialText")</p>
                     <b>@item.GetPropertyValue("testifierName")</b> - <i> @item.GetPropertyValue("testifierCompany")</i>
                     </div>
                     </div>
            }
       }
       }
       }
    

    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?

  • Matt Barlow 118 posts 395 karma points
    27 days ago
    Matt Barlow
    0
    // 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>
        }
    }
    
  • Mark Watson 82 posts 253 karma points
    26 days ago
    Mark Watson
    100

    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

        @helper renderTestimonial(IPublishedContent testimonial)
    {
        var Testimonial = Model.Content.Site().FirstChild("Testimonial").Children();
        var companyLogo = Umbraco.TypedMedia(testimonial.GetPropertyValue<string>("testifierCompanyLogo"));
        if (Testimonial != 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 src="@companyLogo.Url" />
                    }
                    <a href="@testimonial.Url"></a>
                </div>
            </div>
        }
    }
     @{ 
        // 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();
    
      // get 1 random car park testimonial
        var CarParkTestimonial = Model.Content.Site().FirstChild("Testimonial").Children().Where(x => x.HasValue("testimonialCatagory") && x.GetPropertyValue<string>
                   ("testimonialCatagory").Equals("Car Park")).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 (Model.Content.Id == 1089 && CarParkTestimonial != null)
        {
            @renderTestimonial(CarParkTestimonial)
        }
       else if (randomTestimonial != null)
        {
            @renderTestimonial(randomTestimonial)
        }
    
  • Matt Barlow 118 posts 395 karma points
    26 days ago
    Matt Barlow
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft