1

I'm currently working on a template that uses multiple channels (ie. channel="channel1|channel2" to show content. Each channel has a different field group–so I'm conditionally nesting each groups fields within if statements:

{exp:channel:entries
   channel="channel1|channel2"
   disable="category_fields|member_data|pagination|trackbacks"
   dynamic="no"
   limit="1"
   require_entry="yes"
   url_title="{last_segment}"
}
{if channel_id == 1}
   ...
{/if}

{if channel_id == 2}
   ...
{/if}
{/exp:channel:entries}

I was wondering if there are any drawbacks to this?

5

Unless the formatting is very different between the two, you don't need to use the {if}{/if} statement. If the formatting is basically the same, I believe you can stack them like {news_content}{book_content} the fields not related to the entry won't display.

But I use the {if}{/if} approach when the format needs to be handled differently. There's a performance hit (more queries), but it shouldn't be too much.

| improve this answer | |
  • Make senses. I'm using both approaches with showing custom fields and doing some conditionals specific to each group. Grouping them into conditionals makes it a little cleaner, but takes a little performance hit. – Siebird Dec 13 '12 at 16:16
3

If you are worried about the performance hit I would recommend using IfElse by Mark Croxton. It's basically conditionals on steroids because it only parses content if the conditional is met, unlike the baked in conditionals which parse everything and then only display it if the conditional is met.

| improve this answer | |
  • 1
    I believe this statement is only true for Advanced Conditionals, not Simple Conditionals like those in the code example. Correct me if I'm wrong. – Anna_MediaGirl Dec 13 '12 at 16:44
  • Mediagirl, you are correct, but it's really easy to make a small change causing a simple conditional to become an advanced conditional. Better safe than sorry IMO. – CreateSean Dec 13 '12 at 16:48
  • 1
    For example, if within a given channel, a conditional is used to determine whether a given field has been filled in, the conditional is then advanced. If, god forbid, this is necessary within more than one of those channels, each channel conditional becomes advanced. Wrapped with IfElse or Switchee, the "parent" channel conditional would remain simple conditional-like in terms of performance, even if there was a conditional within each since only the matching case would be parsed. – Jean St-Amand Dec 13 '12 at 20:52
3

Judging from you have limit set to 1 and using the last_segment url title I am assuming this is just a detail page for one item and not a full listings page.

You could use the channel_id as above and use switchee or ifelse if the field groups are all completely different and have different case blocks for each channel.

    {exp:switchee variable = "channel_id" parse="inward"}

    {case value="12"}

Or if the majority of the fields are shared and only a small amount of unique fields per different channel field group and you still want to use separate field groups you can use stash to prefix the field based on channel (to use a standard formatting but to swap to unique fields). Mark Croxton submitted this example here:

    {stash:embed file_name="product_loop" name="product_loop_{segment_2}" replace="yes" process="end"}
    {exp:stash:set_list name="products" parse_tags="yes" parse_depth="2"}
    {exp:channel:entries channel="{segment_1}"}
    {exp:stash:set name="channel"}{channel_short_name}{/exp:stash:set}
    {!-- set ALL your product fields here--}
    {exp:stash:set name="channel1_price"}{channel1_price}{/exp:stash:set}
    {exp:stash:set name="channel2_price"}{channel2_price}{/exp:stash:set}

    {stash:this_product_price}{exp:stash:get name="{channel_short_name}_price"}    {/stash:this_product_price}
    {/exp:channel:entries}
    {/exp:stash:set_list}  

In that situation through since most of the fields were similar it made more since (thanks to Mark's comments) to just use one fields group and hide the ones that do not pertain to that channel in the admin to alleviate extra conditionals like above.

I am not sure how your URL structure is before the last segment but for cat listings pages I have a separate template group (so the cat short name is always the first segment for listings pages) that shares one standard view. Then one standard detail page (not related to cat, just product/product-name) that will then use the Switchee similar to above to display the custom fields based on channel.

| improve this answer | |
2

The performance hit is minimal and nothing to worry about IMO. Using the conditionals as you have them coded is the way to do what you're trying to do in the template.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.