Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-06-25 10:08:22

lonelytraveller
Member
From: Firenze, Italy
Registered: 2008-04-04
Posts: 45

Conditional to check if article list is not empty?

Hello, maybe it’s a stupid question, but I’ve been trying to find an answer for so long and probably I’m missing some very obvious point… however here’s my problem:

I’m showing a list of articles in a sidebar (it’s an event list) built wit a simple article_custom tag: <txp:article_custom time="future" limit="5" section="events"> that’s the easy part.
Here’s the difficult part: if the list is empty (that is there’s no event on schedule) then I want a short message to be displayed.
Is there any (easy) solution? I just need to find a way to check if the list is empty (or from another perspetctive if there’s at least one article in the list) but no textpattern conditional tag seems to fit my needs.

Any suggestion would be greatly appreciated.

Offline

#2 2011-06-25 10:21:58

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,011
Website GitHub Mastodon Twitter

Re: Conditional to check if article list is not empty?

Hi Caterina

here’s an untested one:)

<txp:variable name="hasarticles" value='<txp:article_custom time="future" limit="1" section="events" />' />
<txp:if_variable name="hasarticles" value="">
<p>your short message here</p>
<txp:else />
<txp:article_custom time="future" limit="5" section="events" />
</txp:if_variable>

Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#3 2011-06-25 10:48:26

CeBe
Plugin Author
From: Caen - Fr
Registered: 2010-06-25
Posts: 345
Website

Re: Conditional to check if article list is not empty?

Another one, using the plugin chh_if_data

<txp:chh_if_data>
  <txp:article_custom time="future" limit="5" section="events" />
<txp:else />
  your short message
</txp:chh_if_data>

Offline

#4 2011-06-25 11:28:37

lonelytraveller
Member
From: Firenze, Italy
Registered: 2008-04-04
Posts: 45

Re: Conditional to check if article list is not empty?

Colak thanks for reminding me of the @txp:variable tag, I usually forget to use it, but it would make my life much easier. I tested your untested solution and it works like a charm :-)

CeBe thanks for the suggestion, I’ve never used this plug-in but I’ll test your solution too.

Last edited by lonelytraveller (2011-06-25 11:28:57)

Offline

#5 2011-06-25 20:25:09

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Conditional to check if article list is not empty?

chh_if_data does about the same, but it is less flexible than the variable/if_variable combo. And the plugin is not maintained anymore (though it still works).

Offline

#6 2011-06-27 15:47:05

merz1
Member
From: Hamburg
Registered: 2006-05-04
Posts: 994
Website

Re: Conditional to check if article list is not empty?

chh_if_data is an oldie but goldie :)


Get all online mentions of Textpattern via OPML subscription: TXP Info Sources: Textpattern RSS feeds as dynamic OPML

Offline

#7 2011-06-27 15:58:11

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

Re: Conditional to check if article list is not empty?

cough chh_if_data functionality should be core cough

Offline

#8 2011-06-27 16:17:46

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

Re: Conditional to check if article list is not empty?

mrdale wrote:

cough chh_if_data functionality should be core cough

If one wants to use it, use it. It’s available as a plugin, and adding it to the core wouldn’t change how it works. Core has its big brother, variables. More flexible, fast, easy to use and doesn’t share the limitations.

Offline

#9 2011-06-27 16:38:25

CeBe
Plugin Author
From: Caen - Fr
Registered: 2010-06-25
Posts: 345
Website

Re: Conditional to check if article list is not empty?

Gocom a écrit:

Core has its big brother, variables. More flexible, fast, easy to use and doesn’t share the limitations.

Right. So, to take full advantage of speed, let’s use hasarticle variable this way and avoid a second database request :

<txp:variable name="hasarticles" value='<txp:article_custom time="future" limit="1" section="events" />' />
<txp:if_variable name="hasarticles" value="">
  <p>your short message here</p>
<txp:else />
  <txp:variable name="hasarticles" />
</txp:if_variable>

Offline

#10 2011-06-27 18:11:35

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

Re: Conditional to check if article list is not empty?

CeBe wrote:

So, to take full advantage of speed, let’s use hasarticle variable this way and avoid a second database request

Exactly. Good one :)

Offline

Board footer

Powered by FluxBB