Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2020-09-02 10:02:57

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,137
GitHub

Best practice - check for live articles in a specific context

Using Textpattern 4.8, what’s the best practice route for checking if there are live articles in a given context (e.g. category1)?

The old school way I did this, back when I needed to, involved using an article form that output a 1 if the article was live, and nothing if it was any other status. I used an article__custom tag to output all articles in a given section, with optional category1 and/or category2 checks, and dumped that to a variable, which was then empty (i.e. no live articles) or not empty (i.e. one of more live articles). I could then check the variable and do things based on whether there were articles (or not). A bit clunky, but it worked.

This has a faint whiff of being yield and if_yield territory these days, but I’m waaaaaay out of the loop on recent Textpattern tag stuff, so this might be off track.

How would you do it?

Offline

#2 2020-09-02 10:30:39

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Best practice - check for live articles in a specific context

gaekwad wrote #325726:

How would you do it?

No breaking news here, you still have to query db. Few general principles are

  • reuse: if you need to output these articles later, store the complete block rather than 1 in <txp:variable name="has_articles" />. You can test it for emptiness with <txp:if_variable name="has_articles" value /> and output <txp:variable name="has_articles" /> rather than querying db again.
  • or even don’t store it but use <txp:evaluate test="article_custom" />:
<txp:evaluate test="article_custom">
    <txp:tags_to_output_conditionally />
    <txp:article_custom category="..." />
</txp:evaluate>

Offline

#3 2020-09-02 11:44:35

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,137
GitHub

Re: Best practice - check for live articles in a specific context

Thank you, etc – wonderful. evaluate to the rescue!

Offline

Board footer

Powered by FluxBB