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.

Categories versus entries: am I using the right mental model?

September 13, 2008 2:11am

Subscribe [3]
  • #1 / Sep 13, 2008 2:11am

    Andrew Hedges

    12 posts

    I’m deploying EE on a client site and I feel like I’m struggling against the software to do something that should be very basic. In my experience, this often means I am applying the wrong mental model to the problem at hand.

    I have set up categories (developers, resources, etc.) within a weblog (members). I have then posted entries (“Meeting Materials”, “Contacts”) and assigned a single category to each of them. So, there are multiple entries with the title “Contacts”, each assigned to a different category.

    I am generating the section navigation from the category list, which is great. I am even generating secondary navigation from the entries with that category assigned to them. Sweet as.

    The problem comes when I am in either a category page or an entry and want to determine what category I’m dealing with (for purposes of highlighting nav, etc.). Here’s the code that generates my section nav:

    {exp:weblog:categories weblog="members"}
      {category_name}
    {/exp:weblog:categories}

    How would I conditionally add a class=“active” to the span tag for the current category?

    Or… should I switch things around and use entries where I’m currently using categories? This should be easy peasy, but it’s throwing me for a loop. Help!

  • #2 / Sep 13, 2008 7:30am

    Sue Crocker

    26054 posts

    Andrew, have you seen this entry:

    Dynamic CSS Navigation

    I’m not saying this is the perfect solution, but I’ve used it before on client sites.

  • #3 / Sep 13, 2008 2:32pm

    mrwarren

    31 posts

    Seems like you could use an if in there comparing the {category_url_title} to the corresponding segment. Try this?

    {if category_url_title == "{segment_3}"} class="active"{/if}><span>{category_name}</span></a>
  • #4 / Sep 13, 2008 4:28pm

    Andrew Hedges

    12 posts

    Yes, it looks like the URL segment variables will work for my purposes. I’m not sure how I missed them in the documentation.

    Thanks Sue and Brian!

  • #5 / Sep 13, 2008 4:53pm

    Lisa Wess

    20502 posts

    Great suggestions!

    As a note, you don’t want to quote and brace your segment variables.  Read Troubleshooting Conditionals for why.  The security note at the bottom definitely pertains to doing this with segments.

    Instead, try this code:

    {if category_url_title == segment_3} class="active"{/if}><span>{category_name}</span></a>
  • #6 / Sep 13, 2008 4:53pm

    mrwarren

    31 posts

    Oops. My bad. Thanks Lisa.

  • #7 / Sep 13, 2008 4:56pm

    Lisa Wess

    20502 posts

    No worries - it’s a great code snippet; just wanted to make sure it was also secure. =) Thanks for stepping in with the solution!

  • #8 / Sep 13, 2008 6:56pm

    mrwarren

    31 posts

    This is a little ironic. Just a few minutes ago, I ended up using pretty much this exact same code for setting the active class in a category listing.

  • #9 / Sep 14, 2008 12:17pm

    Robin Sowell

    13255 posts

    😉  It is a very handy approach.  Thanks for sharing it- and Lisa for tweaking it!

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

ExpressionEngine News!

#eecms, #events, #releases