The |unique
filter returns the value of the tag that is is applied to only if it is the first time that that tag is encountered.
It is therefore applied to a tag located inside a loop.
In order to differentiate several independent uses within the loop, it is possible to pass a naming argument to this filter.
For example: [(#ID_SECTEUR|unique{en_tete})]
will not have any impact on (will not affect the display of): [(#ID_SECTEUR|unique{corps})]
.
The filter also accepts a second argument: "1" to indicate the number of times that the tag has been filtered.
For example : [(#ID_SECTEUR|unique{corps, 1})]
will display the total corresponding to the number of times where #ID_SECTEUR would have been filtered by |unique{corps}
.
However, we would normally prefer this (more economical) notation for the specific #TOTAL_UNIQUE{corps}
tag [1]
- This |unique
filter can be interesting for displaying a list of articles in date order:
<BOUCLE_blog(ARTICLES) {par date} {inverse} {"<br>"}>
[<hr><h1>(#DATE|affdate_mois_annee|unique)</h1>]
#TITRE ...
</BOUCLE_blog>
In this case, using the filter means that the date will only be displayed when the month changes.
- Another example:
<BOUCLE_blog2(ARTICLES) {par date} {inverse}>
[<hr><h1>(#DATE|annee|unique)</h1>]
[<h2>(#DATE|affdate{'Y-m'}|unique|nom_mois)</h2>]
<a href="#URL_ARTICLE">#TITRE</a><br />
</BOUCLE_blog2>
which will display a list looking like:
2005
March
An article from March
Another article from March
February
An article from February
2004
December
An article from December
In this last example, we use the affdate{'Y-m'}
function to display the month name each time the year changes. In fact:
- if we only coded
[(#DATE|nom_mois|unique)]
, then the month names would only display for the first year; - if the filtering were instead
[(#DATE|unique|nom_mois)]
, then we would display all of the dates. Actually,#DATE
returns a complete date which also contains the hours, minutes and seconds. IT is therefore highly likely that the full dates of two articles published on the same day are actually different from each other.
This is why we have only kept the month and year of the date before passing the result to the unique filter.