Go to main content

If different

Contents

Syntax

<txp:if_different>

The if_different tag is a conditional tag and always used as an opening and closing pair, like this…

<txp:if_different>
    …contained statements…
</txp:if_different>

The tag will output the contained statement when the value of the contained statement differs from the preceding value for that contained statement. Can be used in Textpattern ‘article’, ‘link’, ‘comment’, and ‘file’ type Form templates.

Note: if_different can contain several HTML and Textpattern tag.

Attributes

id="string" v4.8.6+
An identifier for the contained statement value storage.
Values: any string.
Default: unset (the contained statement hash).
test="value" v4.7.2+
If set, the contained statement will be output when the value of test changes.
Values: generally a combination of Textpattern tags.
Default: unset (watch for changes the contained statement itself).

Examples

Example 1: Display posting time per article once per day

<txp:if_different>
    <h3>
        <txp:posted format="%d %B %Y" />
    </h3>
</txp:if_different>

To be used inside Textpattern ‘article’ type Form templates or an article tag container (such as article or article_custom).

Other tags used: posted.

Example 2: Build an indented list of article titles grouped by section

Intention:

  • Display a list of all articles’ titles grouped by sections and ordered by article title.
  • Add headings from section titles to designate an article’s context.
  • Sort alphabetically by section name, then by article title.

Desired result:

  • about (section title)
  • 1st Article from about section
  • 2nd Article from about section
  • …another article
  • family (section title)
  • 1st Article from family section
  • 2nd Article from family section
  • …another article
  • people (section title)
  • 1st Article from people section
  • 2nd Article from people section
  • …another article

In Textpattern Page templates, add this tag to loop through all articles from all sections:

<txp:article_custom sort="Section asc, Title asc">
    <txp:if_different>
        <h2>
            <txp:section title="1" />
        </h2>
    </txp:if_different>
    <h3>
        <txp:title />
    </h3>
</txp:article_custom>

The snippet above lists all article titles and renders an intermittent heading element whenever a different section is encountered while the articles loop through.

Other tags used: article_custom, section, title.

Example 3: Build a <hr />-separated list of article titles grouped by section

Desired result:

  • 1st Article from 1rst section
  • 2nd Article from 1rst section
  • …another article
  • Horizontal rule
  • 1st Article from 2nd section
  • 2nd Article from 2nd section
  • …another article
  • Horizontal rule
  • 1st Article from 3rd section
  • 2nd Article from 3rd section
  • …another article

In Textpattern Page templates, add this tag to loop through all articles from all sections:

<txp:article_custom sort="Section asc, Title asc">
    <txp:if_different test='<txp:section />'>
        <txp:if_first_article not>
            <hr />
        </txp:if_first_article>
    </txp:if_different>
    <h3>
        <txp:title />
    </h3>
</txp:article_custom>

Other tags used: article_custom, if_first_article, section, title.

Genealogy

Version 4.7.2

test attribute added.

Version 4.8.6

id attribute added.

If you notice any kind of problem with this page's construction or content (outdated information, typos, broken links, or whatever), open an issue to have it sorted. Or have a go at it yourself. :)