We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin request "if different"

Development and Programming

NKT com's avatar
NKT com
124 posts
16 years ago
NKT com's avatar NKT com

There is an awesome tag in Textpattern called if_different. I use it all the time to sort loops of entries (or any looped output) into grouped headers.

It basically gets the previous variable it contained and output from the loop and compares it to current variable it’s about to output, if the value is different will it output the contents.

The best part is you don’t need to rely on developers to add header tags to their plugins as it naturally worked in any looping data tags.

Examples…

{exp:weblog:entries weblog="blog" status="open" limit="999" dynamic="off"}
{exp:if_different}<h1>{custom_field}</h1>{/exp:if_different}
<a href="http://{url_title_path=/blog/}">{title}</a>
{/exp:weblog:entries}

Bikes * Bike Type 1 * Bike Type 2 * Bike Type 3 * Bike Type 4 Cars * Car Type 1 * Car Type 2

{exp:custom_module}
{exp:if_different}<h1>{custom_date format="%Y"}</h1>{/exp:if_different}
{exp:if_different}<h2>{custom_date format="%M"}</h2>{/exp:if_different}
{custom_date format="%d %h"} - {title}
{/exp:custom_module}

2009 January day hour - Event 1 day hour - Event 5 Febuary day hour - Event 4

I relies date and category heading tags can sort of achieve this but what if you want to group by another value or group in a tag that doesn’t support those methods?

       
Ingmar's avatar
Ingmar
29,245 posts
16 years ago
Ingmar's avatar Ingmar

You can certainly do something like that with a few lines of PHP:

<?php $prev = ""; $curr = ""; ?>

{exp:weblog:entries ...}

<?php $prev = $curr;          // store the previous value 
$curr = "{my_custom_field}";  // assign current value
if ($curr != $prev) {         // now compare: if current value == previous value, not different

... // Yes, it's different!

}?>
{/exp:weblog:entries}

Makes sense?

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.