3

I have some code inside a global snippet that I only want to display on a blog listing page. So the allowed URL types could be:

  • /blog
  • /blog/P10
  • /blog/category/crackers
  • /blog/author/skeletor

I do not want the code to display on the individual blog post pages (/blog/my-post-title).

So the pseudo-conditional would be:

  • If segment_1 equals "blog" AND

    • segment_2 is empty
    • OR segment_2 is pagination
    • OR segment_2 equals "category"
    • OR segment_2 equals "author"

The pagination is the part that's tripping me up. I know I can use Switchee to use regexes, but it doesn't let me use advanced AND/OR and () operators to string cases together. Something like:

{if segment_1 == 'blog' AND (segment_2 == '#^P(\\d+)$#' OR segment_2 == 'category' OR segment_2 == 'author' OR segment_2 == '')}

Is there an add-on that will let me use regexes inside advanced conditionals, or is there another way of doing it altogether?

2
  • You can do things like AND stackoverflow.com/a/469951 I'd continue w/ Switchee or does it not allow this?
    – Natetronn
    Dec 21 '12 at 19:26
  • It's not just AND/OR though, it's also the () which groups the segment_2 conditionals together. So it would be something like {if segment_1 == 'blog' AND (segment_2 == '#^P(\\d+)$#' OR segment_2 == 'category' OR segment_2 == 'author' OR segment_2 == '')}
    – kmgdev
    Dec 21 '12 at 19:32
5

You can actually nest Switchee tags with a special syntax, so this should work for you:

{exp:switchee variable="{segment_1}" parse="inward"}
    {case value="blog"}
        {switchee variable="{segment_2}" parse="inward"}
            {case value="#^P(\d+)$#|''|category|author"}
            .. display your stuff ...
            {/case}
        {/switchee} 
    {/case}
{/exp:switchee}
4
  • 1
    I did not know that! Great, answer - thanks!
    – kmgdev
    Dec 21 '12 at 19:35
  • I just tried it with a slightly different syntax, but the content is displayed on the single-entry blog post pages. Any idea why?
    – kmgdev
    Dec 21 '12 at 20:01
  • My bad, I was using "case" instead of "variable" inside the {exp:switchee} tag.
    – kmgdev
    Dec 21 '12 at 20:34
  • 1
    Glad you sorted it. Dec 21 '12 at 23:01

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.