Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-07-21 13:13:52

pompilos
Member
From: Spain
Registered: 2005-06-07
Posts: 114
Website

How to hide a file category that is empty?

I have a big list of files to downoad (quite 200) ordered by categories and subcategories. The code I used is very simple:

<txp:category_list type="file" form="downloads" children="0" break="" />

The problem is that some categories and subcategories are empty, because my users add and remove their files freely. What I want is that empty categories do not appear in the list, but I don´t know what is the code I have to use.

I founded this conditional code to hide sections with no articles, but I don´t know to modify it in order to hide categories and subcategories of files.

<txp:section_list break="p">
<!-- check if there are any articles in this section -->
<txp:variable name="hasarticle" value='<txp:article_custom section=''<txp:section />'' limit="1" />' />
<txp:if_variable name="hasarticle" value="">
<!-- no articles in this section, don't display the section -->
<txp:else />
…

Any help? Thanks in advance.

Offline

#2 2009-07-29 23:34:25

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

Re: How to hide a file category that is empty?

Not tested, but you might give this a try:

EDIT: removed code, mixing file categories and link tags is not such a good idea…

Last edited by els (2009-08-03 20:18:36)

Offline

#3 2009-08-03 15:31:04

pompilos
Member
From: Spain
Registered: 2005-06-07
Posts: 114
Website

Re: How to hide a file category that is empty?

Hello, Els. Here is the (wrong) result of your proposal. As I am not very goog with code, I can’t figure where is the error. Any other idea? And thanks, of course, for your effort.

Last edited by pompilos (2009-08-12 10:28:21)

Offline

#4 2009-08-03 17:02:25

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

Re: How to hide a file category that is empty?

Oops, sorry about that!

EDIT: my sincere apologies for posting nonsense code in my previous post… But hang on, I’m working on it and I’m almost there…

Last edited by els (2009-08-03 20:20:52)

Offline

#5 2009-08-03 21:01:24

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

Re: How to hide a file category that is empty?

Got it! :)

<txp:category_list type="file" children="0" break="">
	<!-- check if this parent category or it's children contain one or more files -->
	<txp:variable name="check1" value='<txp:file_download_list category=''<txp:category_list type="file" parent=''''<txp:category />'''' break=","><txp:category /></txp:category_list>'' limit="1"><txp:file_download_name /></txp:file_download_list>' />
	<txp:if_variable name="check1" value="">
	<txp:else />
		<h2><txp:category title="1" /></h2>
		<txp:category_list type="file" parent='<txp:category />' exclude='<txp:category />' break="">
			<!-- check if this child category contains one or more files -->
			<txp:variable name="check2" value='<txp:file_download_list category=''<txp:category />'' limit="1"><txp:file_download_name /></txp:file_download_list>' />
			<txp:if_variable name="check2" value="">
			<txp:else />
				<h3><txp:category title="1" /></h3>
				<ul>
				<txp:file_download_list break="li" category='<txp:category />'><txp:file_download_link><txp:file_download_name /></txp:file_download_link></txp:file_download_list>
				</ul>
			</txp:if_variable>
		</txp:category_list>
	</txp:if_variable>
</txp:category_list>

Phew! This three level deep nesting of tags in attributes gave me a really hard time…

Offline

#6 2009-08-03 21:11:35

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

Re: How to hide a file category that is empty?

Els wrote:

Phew! This three level deep nesting of tags in attributes gave me a really hard time…

Go girl, go! If there was a TXP award for “most awesome example of nested tags” you’d be the clear winner. I think I need a beer and I merely had to read the code… ;-)

Last edited by Bloke (2009-08-03 21:12:01)


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-08-03 21:36:20

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

Re: How to hide a file category that is empty?

Bloke wrote:

I think I need a beer and I merely had to read the code… ;-)

:-D If you need one then I definitely deserve one ;) (and off I go to find myself a nice cold Jupiler…)

Offline

#8 2009-08-04 00:38:16

tye
Member
From: Pottsville, NSW
Registered: 2005-07-06
Posts: 859
Website

Re: How to hide a file category that is empty?

This should work with article catgeories shouldn’t it? (With a bit of tweaking)

Its wierd… if I have an image category with no images, it doesn’t display. But if I have an article (or it seems file) category – it does.

Is there a reason for this?

Offline

#9 2009-08-04 00:51:47

tye
Member
From: Pottsville, NSW
Registered: 2005-07-06
Posts: 859
Website

Re: How to hide a file category that is empty?

Actually – that is a total lie – it is smd_gallery which ignores empty categories…

It would be useful to included a tag called something like “showemptycats=“0/1” or something along those lines

Offline

#10 2009-08-04 10:14:17

tye
Member
From: Pottsville, NSW
Registered: 2005-07-06
Posts: 859
Website

Re: How to hide a file category that is empty?

Ah – no worries – I found this thread which does something similar… although I’m still not getting the variable thing and how it is checking :(

Offline

#11 2009-08-07 13:32:08

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

Re: How to hide a file category that is empty?

Hey pompilos, I just checked your page and noticed that some (but not all) ‘empty’ parent categories are still being displayed. I suspect that is because there are files in those parent categories, and in my code I assumed that files would all be in the child categories, therefore the files in parent categories are not being displayed.
Please let me know if I’m right and if you want me to adapt the code.

Offline

#12 2009-08-07 14:37:02

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

Re: How to hide a file category that is empty?

(Edited: trimmed the code a little bit, I realized I made it more complicated than necessary.)

OK, here’s the code that displays the files in parent categories as well.
I added notextile. to get rid of the bunch of validation errors caused by the <p> tags wrapped around the list ;-)

notextile. <txp:category_list type="file" children="0" break="">
	<!-- check if this parent category or it's children contain one or more files -->
	<txp:variable name="check1" value='<txp:file_download_list category=''<txp:category_list type="file" parent=''''<txp:category />'''' break=","><txp:category /></txp:category_list>'' limit="1"><txp:file_download_name /></txp:file_download_list>' />
	<txp:if_variable name="check1" value="">
	<txp:else />
		<h2><txp:category title="1" /></h2>
		<txp:file_download_list wraptag="ul" break="li" category='<txp:category />'><txp:file_download_link><txp:file_download_name /></txp:file_download_link></txp:file_download_list>
		<txp:category_list type="file" parent='<txp:category />' exclude='<txp:category />' break="">
			<!-- check if this child category contains one or more files -->
			<txp:variable name="check2" value='<txp:file_download_list category=''<txp:category />'' limit="1"><txp:file_download_name /></txp:file_download_list>' />
			<txp:if_variable name="check2" value="">
			<txp:else />
				<h3><txp:category title="1" /></h3>
				<txp:file_download_list wraptag="ul" break="li" category='<txp:category />'><txp:file_download_link><txp:file_download_name /></txp:file_download_link></txp:file_download_list>
			</txp:if_variable>
		</txp:category_list>
	</txp:if_variable>
</txp:category_list>

Last edited by els (2009-08-08 22:37:52)

Offline

Board footer

Powered by FluxBB