2

This is something that I'm wondering how you solve it:

When building a site, the index template is served with segment 1 empty. If I start an entries loop in a embed, I'll need a conditional to show the specific category for the home page and automated categories for the rest like so:

{if segment_1==""}
    {exp:channel:entries category="1"}
        All kinds of channel entry stuff
    {/exp:channel:entries}
{if:else}
    {exp:channel:entries}
        All kinds of channel entry stuff
    {/exp:channel:entries}
{/if}

The only difference between the first and second loop is the category, which is called automatically in the second loop by the URL segment. Every time I make a change in the first loop, I have to be careful to do that in the second one too. I could put the 'entry stuff' in a snippet, but I like to work with files, which snippets do not allow.

So how would you approach this? Thanks!

4

You can wrap parameters of your channel:entries tag in simple conditionals.

So instead of duplicating your channel loop (which is not very DRY), just use one loop and put the category parameter inside a conditional:

{exp:channel:entries 
    {if segment_1 == ""}category="1"{/if}
}
    {!-- channel entries template stuff --}
{/exp:channel:entries}

Or if you did want to use a snippet for the repeated template code yet still reap the benefits of working with templates as files you can use Snippet Sync. I use it on all my installs and it works great. There is a developer license available.

5
  • Are you sure that's doable with parse order? The reason why I started using this bulky method is because I was unable to put the entries tag in a conditional. Like: {if segment_1==""}{exp:channel:entries}{if:else}{exp:channel:entries category="1"}{/if}stuff{/exp:channel:entries} But I have to say that was still with EE1. Thanks for the lead to snippet sync! – noregt Jan 22 '13 at 20:23
  • What you describe in your comment is wrapping just the opening tag in in advanced conditional which isn't going to work (as you said). But my example uses a simple conditional around just a parameter in the opening exp:channel:entries tag and should work fine. I have done similar many times in templates where I want a single channel entries loop to serve multiple purposes. – Alex Kendrick Jan 22 '13 at 21:03
  • Got it working, but had to restrict it to conditionals without if:else, instead used double if statements. Thanks! – noregt Jan 22 '13 at 22:30
  • You could also keep both loops and use switchee or iflese by Mark Croxton to test your segment values. Only the tags inside the matching condition will remain in the template to be parsed. No overhead. – Jérôme Coupé Jan 23 '13 at 11:28
  • Indeed. Switchee provides some great benefits and if the channel:entries innards were not identical that would be the way to go. But the original aim here was to avoid identical, duplicate loops from a DRY standpoint. – Alex Kendrick Jan 23 '13 at 15:16

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.