ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

EE Templates - is there a better alternative to multiple {if} statements?

May 09, 2012 11:41am

Subscribe [2]
  • #1 / May 09, 2012 11:41am

    martinj

    32 posts

    Hi all, I’m looking for the most DRY solution for coding a template with some specific functionality.

    The Scenario
    The first channel is “Private Water Supplies” - there’s an entry for each type of customer, giving a general description of services. The entries are:

    Domestic
    Agriculture
    Industry
    Property Development
    Hotels
    Offices
    Government
    Sports Clubs
    Education


    The second channel is “Testimonials” - entries are categorised by type of customer, the categories match the Private Water Supply entries, i.e.:

    Domestic
    Agriculture
    Industry
    Property Development
    Hotels
    Offices
    Government
    Sports Clubs
    Education

    My Private Water Supplies template needs to pull one random testimonial for each entry from the Testimonial channel entries with the appropriate category. I.e.:

    private-water-supplies/domestic displays one testimonial at chosen at random from all testimonials in the domestic category; private-water-supplies/education displays one testimonial at chosen at random from all testimonials in the education category; etc.


    Current Solution
    My Private Water Supplies template currently uses the following code for the domestic testimonial category:

    {if segment_2 == 'domestic'}
        <blockquote class="testimonial">
        {exp:channel:entries channel="testimonials" disable="member_data|pagination" limit="1" category="3" orderby="random" dynamic="no"}
            {testimonial_main_content}
            <h6 class="caption">{if testimonial_client_name}{testimonial_client_name}, {/if}{if testimonial_position}{testimonial_position}, {/if}{if testimonial_company}{testimonial_company}, {/if}{if testimonia_location}{testimonia_location}{/if}</h6>
        {/exp:channel:entries}
        </blockquote>
    {/if}

    This code will have to be repeated using {if:ifelse} statements for each of the other 8 testimonial categories, changing the

    category="3"

    for each category.


    Question
    Is there a way I can automate the {if} statement so that testimonials in the appropriate category are displayed based on the segment_2 value - or maybe there’s another way to achieve the same result?

    Thanks in advance for any suggestions.

     

  • #2 / May 09, 2012 11:50am

    Steve Abraham

    149 posts

    Yep, you want Low’s Seg2Cat add-on, which will give you the category_id (along with other things) for any given segment:
    http://gotolow.com/addons/low-seg2cat

  • #3 / May 09, 2012 12:02pm

    martinj

    32 posts

    Thanks for the quick reply Steve, much appreciated.

    I should have made clear, given that I’m still learning EE I’d *really* like to learn how to achieve this using native functionality - if it’s possible. I’ll definitely check out the Seg2Cat addon though.

  • #4 / May 09, 2012 12:09pm

    Steve Abraham

    149 posts

    Totally understand you wanting to do it core. In this instance however any core method you attempt will result in either slightly uglier URLs (using the ID instead of, or as well as, the name), or looping through multiple IF statements.

    If you’re not familiar with Low, he’s one of the big names in EE addons, well trusted, good code and excellent support. His add-ons, like Seg2Cat, grace almost every build I do.

  • #5 / May 09, 2012 12:20pm

    martinj

    32 posts

    Thanks for confirmation Steve - my thinking, as you’ve gathered, is to learn the practical boundaries of the EE Core before turning to addons. Looks like my ugly multiple looping IF statements is the boundary in this case. And not particularly robust either - if the client writes a new entry in Private Water Supplies, creates an associated testimonial category, I’ll still have to modify the template to link up the two…

    I have heard of Low and am aware of his prominence in the EE community. Looks like Seg2Cat will be installed soon.

    Thanks again 😊

  • #6 / May 10, 2012 12:55pm

    Dan Decker

    7338 posts

    Hi Martin,

    Looks like Steve gave you a solid start!

    As he noted, Seg2Cat goes in almost every ExpressionEngine build I do. The great thing about EE is most add-ons work just like Core features. Same syntax, same expectations etc.

    Is there anything else I can assist you with?

  • #7 / May 10, 2012 1:44pm

    martinj

    32 posts

    Hi Dan

    You can go ahead and close up this thread now thanks. Steve’s advice was spot on.

  • #8 / May 15, 2012 12:06pm

    Dan Decker

    7338 posts

    Hi martinj,

    Steve’s advice was spot on.

    Awesome! I love it when the Community comes through!

    If you need anything else later, just let us know!

    Cheers,

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases