Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-06-03 18:16:11

Timid&friendly
Member
From: The Netherlands
Registered: 2006-05-28
Posts: 252
Website

What is [txp:if_different] and how does it work.

First of all please excuse my ignorance. In textbook it says:-

The tag will execute the contained statement when the value of the contained statement differs from the preceding value for that contained statement. Can be used in article, link, comment, and file forms.

The examples are great and i use them, but i still don’t really understand whats happening and how it actually works. Could some of you clever programmers please try to explain in laymans(less asbtract way) terms how it works?


I think, therefore I AM, … … er … I think :-?

Offline

#2 2009-06-03 18:52:25

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: What is [txp:if_different] and how does it work.

If previous contained code is different from current returned code, the code is shown, If it returns same it isn’t. Let’s put it like:

First call: <txp:if_different>Cat | <txp:category /></txp:if_different>
Returns: Cat | general

Second call: [...]
Returns: Cat | general

Third call: [...]
Returns: Cat | web

So we get a matching of those returnings. And if it is different from the previous, then the contained statement is shown. If it is same then it isn’t.

Cat | general == Cat | general != Cat | web

It shows: Cat | general and Cat | web, because the second returned code was identical with the first one.

Hope that explains it better.

Offline

#3 2009-06-03 19:03:15

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,454
Website GitHub

Re: What is [txp:if_different] and how does it work.

Timid&friendly

It’s best explained by example. Take an article list as given by <txp:article_custom section="blog" sort="category1">. It returns a list of articles and each one has a load of “properties” (for want of a better word), e.g. each has a body, an excerpt, a category1, category2, a posted date, and so on.

Inside the container of this article_custom tag, the TXP parser ‘sees’ each article as it goes by. Instead of just displaying the same content from every article, you might want to show them grouped by category1 on the screen, for example. Since we’ve sorted by category1 we can ask the container to only output something when it detects that the category1 in the current article is different from the category1 of the article before it.

In other words, TXP keeps a note of each property in each article and you can ‘watch’ to see when one of those changes as the articles go past TXP’s eyes. When it does, if_different fires and you can display something.

It’s probably more complicated than that and I guess there are some things you can’t watch, but essentially that’s it.

Last edited by Bloke (2009-06-03 19:05:07)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#4 2009-06-03 19:34:56

Timid&friendly
Member
From: The Netherlands
Registered: 2006-05-28
Posts: 252
Website

Re: What is [txp:if_different] and how does it work.

OK that makes a lot of sense :-D

So if i am correct, the tag makes an array of all the values for a given “property”- (the property you want to watch) and if the value is different from the previous values(s) of that property, it outputs all the unique values within that given property AND makes a list of any other related code tide to the parent form. An array within an array right?


I think, therefore I AM, … … er … I think :-?

Offline

#5 2009-06-03 19:47:15

thebombsite
Archived Plugin Author
From: Exmouth, England
Registered: 2004-08-24
Posts: 3,251
Website

Re: What is [txp:if_different] and how does it work.

Yes. It’s a bit like a filter. As you know you can have a load of categories which aren’t necessarily tied to a single section, however with <txp:if_different> you can go through one specific section looking at the categories that are used in that one section and output a list of the categories. Obviously the same category can be used for several articles in that section but it will only be listed once.


Stuart

In a Time of Universal Deceit
Telling the Truth is Revolutionary.

Offline

#6 2009-06-03 19:51:49

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,454
Website GitHub

Re: What is [txp:if_different] and how does it work.

Timid&friendly wrote:

So if i am correct, the tag makes an array of all the values for a given “property”- (the property you want to watch) and if the value is different from the previous values(s) of that property, it outputs all the unique values within that given property AND makes a list of any other related code tide to the parent form. An array within an array right?

Yes. But it’s doing it one article at a time so it outputs stuff as it goes rather than waiting till the end.

Thus if your blog about programming and zookeeping had an article_custom that returned this list of articles (abbreviated content!) :

Title                      Cat1   Posted
---------------------------------------------
Textpattern is great       txp    28 May 2009
4.0.9 is gonna rock        txp    25 May 2009
The new orang-utans        zoo    03 Jun 2009
The penguins have landed   zoo    02 Jun 2009
Giraffe central            zoo    23 May 2009

Then you could just output the stuff one at a time so it looked like that on the screen. But, if instead of displaying category1 inline, you wanted to put the articles under their category headings so it looked like this:

Title                      Posted
---------------------------------------------
txp
Textpattern is great       28 May 2009
4.0.9 is gonna rock        25 May 2009

zoo
The new orang-utans        03 Jun 2009
The penguins have landed   02 Jun 2009
Giraffe central            23 May 2009

if_different could be told to ‘watch’ for changes in cat1. The parser / if_different sees the following, one by one:

1) Textpattern is great : cat1 = “txp”
===> This is different from before (previously cat1 was ‘empty’), I’ll output the cat1

2) 4.0.9 is gonna rock : cat1 = “txp”
===> Hmm, same as before, skip it

3) The new orang-utans : cat1 = “zoo”
===> This is different from before (previously cat1 was ‘txp’), I’ll output the cat1

4) The penguins have landed : cat1 = “zoo”
===> Same as before, skip it

5) Giraffe central : cat1 = “zoo”
===> Same as before, skip it

So it’s kind of doing stuff as it goes rather than building up arrays of arrays. Internally it might be doing the sub-array thing, I don’t know. Never looked!

EDIT: thebombsite was of course quicker, and more succinct :-)

Last edited by Bloke (2009-06-04 15:26:43)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#7 2009-06-04 01:34:22

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: What is [txp:if_different] and how does it work.

Bloke wrote:

EDIT: thebombsite was of course quicker, and more succinct :-)

Nah, “the penguins have landed” was well worth it.

Offline

#8 2009-06-04 15:23:15

Timid&friendly
Member
From: The Netherlands
Registered: 2006-05-28
Posts: 252
Website

Re: What is [txp:if_different] and how does it work.

Thx guyz, i think i understand now how it works and when to use it


I think, therefore I AM, … … er … I think :-?

Offline

Board footer

Powered by FluxBB