Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2017-07-24 10:30:59

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Check if a file exists within a file category

Hi there,

I need some code to check if at least one file exists within a file category, but am unsure how to write the tags to do that.

Basically it needs to check if the file category ‘current-beta-release’ exists, and then do an action if it does. I’m using 4.7dev so the <txp:evaluate> tag is available I just don’t know how to implement it in this situation (or if that tag doesn’t do that, then what combination of tags do).

Offline

#2 2017-07-24 15:49:42

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

Re: Check if a file exists within a file category

Hi Phil,

would this be of help?

<txp:if_category type="files" name="current-beta-release">
<txp:file_download_list limit="5" break="li" wraptag="ol" sort="downloads desc" />
<txp:else />
<p>Nothing to download</p>
</txp:if_category>

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 2017-07-24 16:39:27

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

Re: Check if a file exists within a file category

Rethinking about it:)

<txp:variable name="hasbeta"><txp:file_download_list category="current-beta-release" limit="1" break="" wraptag="" sort="downloads desc" /></txp:variable>
<txp:if_variable name="hasbeta" value="">
<txp:else />
<txp:variable name="hasbeta" />
</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

#4 2017-07-24 18:08:16

kuopassa
Plugin Author
From: Porvoo, Finland
Registered: 2008-12-03
Posts: 229
Website

Re: Check if a file exists within a file category

That’s a nice little brain teaser. :-) I got this to work with 4.6.2:

<txp:php>
if (safe_row('name','txp_category','`name` = "current-beta-release" AND `type` = "file"') == true) {
echo 'exists';
}
else {
echo 'does not exist';
}
</txp:php>

Offline

#5 2017-07-25 10:10:04

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

Re: Check if a file exists within a file category

colak wrote #306422:

Rethinking about it

You can also flip the logic in 4.7.0 by using valueless attributes:

<txp:variable name="hasbeta"><txp:file_download_list category="current-beta-release" limit="1" break="" wraptag="" sort="downloads desc" /></txp:variable>
<txp:if_variable name="hasbeta" value>
   <txp:variable name="hasbeta" />
<txp:else />
   <p>No beta downloads available.</p>
</txp:if_variable>

But jakob nailed it with the <txp:evaluate> tag, which is neater.


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

#6 2017-07-25 10:24:54

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Check if a file exists within a file category

Thanks for the help everyone – I used the <txp:evaluate> version in the end. Final code here.

Offline

Board footer

Powered by FluxBB